我的程序一直在意图崩溃,似乎无法找出错误的原因。有人可以帮忙吗?我试过删除" final"从上下文声明中,有什么我必须手动导入的吗?
public class MainActivity extends AppCompatActivity {
EditText answer;
ImageButton flashcard;
Context context = this;
public void onClick(View v) {
int ans = Integer.parseInt(answer.getText().toString());
if (ans == 5) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Answer is correct");
alert.setMessage("See another?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
else
Toast.makeText(MainActivity.this, "Answer is Incorrect, try again.", Toast.LENGTH_SHORT).show();
}
});
}
}
错误日志在
之下4 11:30:05.048 9569-9569/com.example.exam3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.exam3, PID: 9569
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exam3/com.example.exam3.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.exam3.Main2Activity.onCreate(Main2Activity.java:28)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
Do it this way:
flashCard.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
int ans = Integer.parseInt(answer.getText().toString());
if (ans == 5) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Answer is correct")
.setMessage("See another?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
else
Toast.makeText(MainActivity.this, "Answer is Incorrect, try again.", Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
引起:java.lang.NullPointerException:尝试调用虚拟 方法'void android.widget.ImageButton.setOnClickListener(android.view.View $ OnClickListener)' 在一个空对象上
告诉您Main2Activity在空对象上调用onSetClickListener
,其余的告诉您它发生在onCreate
方法中。最可能的问题是您的layout.xml文件要么没有定义适当的视图,要么具有不同的标识符。很难说,因为它与您发布的代码无关。
答案 2 :(得分:0)
您需要按flashCard
定义findViewById(R.id.flash_card)
是什么。 R.id.flash_card
是xml布局文件中该元素的id。在致电findViewById(R.id.flash_card)
flashCard.setOnClickListener(new View.OnClickListener() { ...});