2013-01-21 67 views
0

我想創建一個對話框,當創建一個活動時彈出。這是我創建對話框的方法:創建對話框時出錯

public Dialog createDialog(){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplication()); 

    builder.setMessage("Order Information"); 
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 
      //Create order object in here 
      Toast.makeText(getApplication(), "order created!", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return builder.create(); 
} 

然後我把它在我的onCreate方法如下所示:

Dialog d = createDialog(); 
    d.show(); 

這是我收到的錯誤:

java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.waitron5/com.example.waitron5.MainActivity}:android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序

產生的原因:android.view.WindowManager $ BadTokenException:無法添加窗口 - 令牌null不是一個應用程序

這是我的日誌貓錯誤:

01-21 16:10:45.533: E/AndroidRuntime(9228): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.os.Looper.loop(Looper.java:137) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at dalvik.system.NativeStart.main(Native Method) 
01-21 16:10:45.533: E/AndroidRuntime(9228): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:571) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.Dialog.show(Dialog.java:281) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at com.example.waitron5.MainActivity.onCreate(MainActivity.java:52) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.Activity.performCreate(Activity.java:5104) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-21 16:10:45.533: E/AndroidRuntime(9228):  ... 11 more 

任何想法我做錯了什麼,我明白它可能是DialogBox沒有得到它創建的活動的引用。我正在考慮將一個對活動的引用存儲在一個靜態變量中,並以此方式調用它,但是我發現由於內存泄漏,不推薦這樣做。

任何意見是非常感謝!

+1

它可能與你使用'getApplication()'的事實做。我認爲你需要活動環境,因爲它有一個窗口。 –

+0

好的我現在就試試 – Javacadabra

回答

1

只要改變

AlertDialog.Builder builder = new AlertDialog.Builder(getApplication()); 

AlertDialog.Builder builder = new AlertDialog.Builder(YourClassName.this); 
+0

非常好,謝謝,問題解決了!將在10分鐘內接受你的答案。 – Javacadabra

+0

這太棒了:) – Rahil2952

+0

或getApplicationContext():)) –