英文:
Why is my toast application not compiling
问题
package com.example.toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fun(View v) {
Toast t;
t = Toast.makeText(this, "Welcome to Android", Toast.LENGTH_LONG);
t.show();
}
}
The words "context" and "resid" are corrected in the code snippet. The corrected code should resolve the issue you mentioned, and the error you were facing should be resolved.
英文:
package com.example.toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fun(View v) {
Toast t;
t = Toast.makeText( context:this, resid: "Welcome to Android", Toast.LENGTH_LONG);
t.show();
}
The words "conext" and "resid" are highlighted in red so I'm not sure what is going on. The code will not compile and I'm struggling to understand why. I am following the instructions word for word from the book.
I am getting this error:
AAPT: error: not well-formed (invalid token).
答案1
得分: 3
你是在观看视频还是图片并复制了代码?
context:
和resid:
是IDE提示,不是方法调用中的实际文本。
英文:
Were you watching a video or image and copied the code?
The context:
and resid:
are IDE hints, not actual text in the method call.
答案2
得分: 1
仅需输入
Toast.makeText(MainActivity.this, "欢迎来到安卓", Toast.LENGTH_LONG).show();
未显示的原因
你在输入"context:"和"resid",但实际上,你只需要这三个参数:
- 上下文(Context)= MainActivity.this(在你的情况下)
- 要显示的文本 = 你的自定义文本
- Toast的显示时长 = 根据情况选择 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG。
英文:
Just Type
Toast.makeText( MainActivity.this, "Welcome to Android", Toast.LENGTH_LONG).show();
Reason For not showing
you were typing out "context:" and "resid" but in fact, you only need the 3 parameters
- Context = MainActivity.this in your case
- Test to show = your custom text
- Duration of Toast = Toast.LENGTH_SHORT/Toast.LENGTH_LONG depending upon your situation.
答案3
得分: 0
Just remove context: and resid: from the arguments. Those are syntax errors in Java, so the code won't compile.
package com.example.toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fun(View v) {
Toast t;
t = Toast.makeText(this, "Welcome to Android", Toast.LENGTH_LONG);
t.show();
}
}
英文:
Just remove context: and resid: from the arguments. Those are syntax errors in Java, so the code won't compile.
package com.example.toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import static android.widget.Toast.LENGTH_LONG;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fun(View v) {
Toast t;
t = Toast.makeText(this, "Welcome to Android", Toast.LENGTH_LONG);
t.show();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论