我的程序不斷崩潰在意圖,似乎無法弄清楚什麼是錯的。誰能幫忙?我試圖從上下文聲明中刪除「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)
「不斷崩潰」並沒有告訴我們多少。發佈您的完整logcat –
發佈logcat – Kaushal28
發佈logcat,以便我們可以找出你的代碼有什麼問題,但我想你沒有將Main2Activity添加到清單文件:/ –