4
我在網上查了很多,現在變得沮喪,因爲我找不到關於以下錯誤的信息。有人可以幫我弄清楚這是怎麼回事?Android中的AlertDialog - 空指針異常
08-17 13:15:47.800: W/dalvikvm(10356): threadid=1: thread exiting with uncaught exception (group=0x401f0760)
08-17 13:15:47.800: E/AndroidRuntime(10356): FATAL EXCEPTION: main
08-17 13:15:47.800: E/AndroidRuntime(10356): java.lang.NullPointerException
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:120)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:337)
08-17 13:15:47.800: E/AndroidRuntime(10356): at com.work.plan.Login$1.onClick(Login.java:64)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.view.View.performClick(View.java:3112)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.view.View$PerformClick.run(View.java:11956)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.os.Handler.handleCallback(Handler.java:587)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.os.Looper.loop(Looper.java:132)
08-17 13:15:47.800: E/AndroidRuntime(10356): at android.app.ActivityThread.main(ActivityThread.java:4025)
08-17 13:15:47.800: E/AndroidRuntime(10356): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 13:15:47.800: E/AndroidRuntime(10356): at java.lang.reflect.Method.invoke(Method.java:491)
08-17 13:15:47.800: E/AndroidRuntime(10356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-17 13:15:47.800: E/AndroidRuntime(10356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-17 13:15:47.800: E/AndroidRuntime(10356): at dalvik.system.NativeStart.main(Native Method)
這裏是導致錯誤的類:
OnClickListener LoginListener = new OnClickListener(){
public void onClick(View arg0) {
String checkUserName = userName.getText().toString();
Cursor mCursor = mDbHelper.fetchUserByName(checkUserName);
if(mCursor.moveToFirst()){
String checkDatabaseUser = mCursor.getString(mCursor.getColumnIndexOrThrow(LoginDbAdapter.KEY_NAME));
Toast.makeText(getBaseContext(), checkDatabaseUser,Toast.LENGTH_LONG).show();
if(checkDatabaseUser.equals(checkUserName)){
Intent i = new Intent();
i.setClass(getBaseContext(), WorkoutPlanActivity.class);
startActivity(i);
}
}else {
**//Error happening here -->** AlertDialog.Builder alert = new AlertDialog.Builder(m_ClassContext);
alert.setTitle("Error Logging in");
alert.setMessage("The user name is not in the system.");
alert.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alert.show();
}
}
};
我改變了警惕這種
AlertDialog alert = new AlertDialog.Builder(getBaseContext()).create();
alert.setTitle("Error Logging in");
alert.setMessage("The user name is not in the system.");
alert.setButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alert.show();
,現在我得到以下錯誤:
08-17 13:38:38.460: W/dalvikvm(10513): threadid=1: thread exiting with uncaught exception (group=0x401f0760)
08-17 13:38:38.460: E/AndroidRuntime(10513): FATAL EXCEPTION: main
08-17 13:38:38.460: E/AndroidRuntime(10513): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.view.ViewRoot.setView(ViewRoot.java:450)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.app.Dialog.show(Dialog.java:269)
08-17 13:38:38.460: E/AndroidRuntime(10513): at com.work.plan.Login$1.onClick(Login.java:74)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.view.View.performClick(View.java:3112)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.view.View$PerformClick.run(View.java:11956)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.os.Handler.handleCallback(Handler.java:587)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.os.Looper.loop(Looper.java:132)
08-17 13:38:38.460: E/AndroidRuntime(10513): at android.app.ActivityThread.main(ActivityThread.java:4025)
08-17 13:38:38.460: E/AndroidRuntime(10513): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 13:38:38.460: E/AndroidRuntime(10513): at java.lang.reflect.Method.invoke(Method.java:491)
08-17 13:38:38.460: E/AndroidRuntime(10513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-17 13:38:38.460: E/AndroidRuntime(10513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-17 13:38:38.460: E/AndroidRuntime(10513): at dalvik.system.NativeStart.main(Native Method)
這最終成爲該解決方案,謝謝你們
private Context m_ClassContext = this;
AlertDialog alert = new AlertDialog.Builder(m_ClassContext).create();
alert.setTitle("Error Logging in");
alert.setMessage("The user name is not in the system.");
alert.setButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alert.show();
你能表現出更多的課嗎?特別是'm_ClassContext'的聲明和定義? – Eric 2012-08-17 17:25:32
是'com.work.plan.Login $ 1.onClick(Login.java:64)'你的一個類嗎?第64行發生了什麼?這是標記線嗎? – micha 2012-08-17 17:29:14
你可以發佈清單xml。也許它與主題 – nandeesh 2012-08-17 17:29:40