點擊按鈕顯示AlertDialog,代碼:
@Override
public void onClick(View arg0) {
Log.d("","Kliknieto");
AlertDialog.Builder b = new AlertDialog.Builder(ctx);
b.setTitle(R.string.camerae);
if(dpm.getCameraDisabled(c))
{
b.setMessage(R.string.enabled);
b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
b.setPositiveButton(R.string.senable, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Some code removed
}
});
}
else
{
b.setMessage(R.string.disabled);
b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
b.setPositiveButton(R.string.sdisable, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Some code removed
}
});
}
b.show();
}
變量聲明正確,但是不知道為什麼會出錯:
08-11 16:24:39.708: E/AndroidRuntime(951): FATAL EXCEPTION: main
08-11 16:24:39.708: E/AndroidRuntime(951): java.lang.NullPointerException
08-11 16:24:39.708: E/AndroidRuntime(951): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
08-11 16:24:39.708: E/AndroidRuntime(951): at com.radzik.devadmin.MainActivity$5.onClick(MainActivity.java:140)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.view.View.performClick(View.java:4084)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.view.View$PerformClick.run(View.java:16966)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.os.Handler.handleCallback(Handler.java:615)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:137)
08-11 16:24:39.708: E/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-11 16:24:39.708: E/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:24:39.708: E/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 16:24:39.708: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-11 16:24:39.708: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 16:24:39.708: E/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
140行是:
AlertDialog.Builder b = new AlertDialog.Builder(ctx);
檢查ctx是否為空,這裡的Context需要與界面關聯,需要使用XXXActivity.this,不能使用全局的Context