0
我的AlertDialog
不起作用,應用程序在啓動時關閉。警報對話框不起作用,強制關閉發射
Button btn_disclaimer = (Button) findViewById(R.id.btn_disclaimer);
btn_disclaimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MyActivity.this);
alertDialogBuilder.setTitle("DISCLAIMER");
alertDialogBuilder
.setMessage("TEST123")
.setNegativeButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
它有什麼問題?我進口了這些傢伙:
import android.app.AlertDialog;
import android.content.DialogInterface;
我是新來的Android,只是創建一些簡單的應用程序。
java.lang.RuntimeException: Unable to start activity ComponentInfo{gd.mkp.jd.de.gymnasiumdamme/gd.mkp.jd.de.gymnasiumdamme.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5140)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at gd.mkp.jd.de.gymnasiumdamme.MyActivity.onCreate(MyActivity.java:279)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2170)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5140)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
從logcat – Karakuri 2014-10-01 15:14:07
發佈一個完整的堆棧跟蹤,你不需要'DialogInterface.OnClickListener',你可以通過'null',並且當用戶點擊好的時候它仍然會關閉對話框 – Karakuri 2014-10-01 15:15:40
你的'onCreate中有一個NPE )'(#279行),而不是在'onClick()'。可能'btn_disclaimer'爲空。將來,請將堆棧跟蹤作爲文本直接發佈到問題中,而不是作爲外部pastebins或圖片。 – laalto 2014-10-01 15:19:25