2011-11-06 87 views
1
AlertDialog.Builder builder; 
    AlertDialog alertDialog; 

    Context mContext = getApplicationContext(); 
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.dialoglayout, 
            (ViewGroup) findViewById(R.id.layout_root)); 

    TextView text = (TextView) layout.findViewById(R.id.text); 
    text.setText("Hello, this is a custom dialog!"); 
    ImageView image = (ImageView) layout.findViewById(R.id.image); 
    image.setImageResource(R.drawable.icon); 

    builder = new AlertDialog.Builder(mContext); 
    builder.setView(layout); 
    alertDialog = builder.create(); 

    alertDialog.show(); 

誰能告訴我這個代碼。它的問題給出了以下異常:的Android警告對話框不工作

11月11日至6日:44:20.572:ERROR/AndroidRuntime(339):致命異常: main 11-06 11:44:20.572:ERROR/AndroidRuntime(339):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.andoroid.dialog/com.andoroid.dialog.AlertDialogTestActivity}:android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 11 -06 11:44:20.572: ERROR/AndroidRuntime(339):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.ActivityThread.access $ 2300(ActivityThread .java:125) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.os.Handler.dispatchMessage(Handler.java:99) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.os.Looper.loop(Looper.java :123) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.ActivityThread.main(ActivityThread.java:4627) 11-06 11:44:20.572:ERROR/AndroidRuntime(339) ):java.lang.reflect.Method.invokeNative(Native Method) 11-06 11:44:20.572:ERROR/An droidRuntime(339):at java.lang.reflect.Method.invoke(Method.java:521) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at dalvik.system.NativeStart.main(Native Method) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):由:android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.view.ViewRoot.setView(ViewRoot.java:509) 11 -06 11:44:20.572:ERROR/AndroidRuntime(339):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.Dialog.show(Dialog.java:241) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at com.andoroid.dialog.AlertDialogTestActivity.createDialog(AlertDialogTestActivity.java:48) 11 -06 11:44:20.572:ERROR/AndroidRuntime(339):at com.andoroid.dialog.AlertDialogTestActivity.onCreate(AlertDialogTestActivity.java:22) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-06 11:44:20.572:ERROR/AndroidRuntime(339):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

回答

1

我的想法:

1)使用當前的活動,而不是mContext = getApplicationContext();例如:

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 

指的是你的活動,如果你在它編寫代碼。

2)清除您的項目

+0

感謝名單了很多隊友,它的工作!你能告訴我使用applicationContext時出錯的原因嗎? – Gaurav

+0

public Context getApplicationContext() 從以下版本開始:API級別1 返回當前進程的單個全局應用程序對象的上下文。這通常只應用於需要生命週期與當前上下文分離的Context,該生命週期與進程的生命週期相關,而不是當前組件。 – breceivemail

1

而且,如果你想有一個自定義對話框沒有必要誇大的看法和使用AlertDialog.Builder。

相反,你可以像這樣做:

Dialog customDialog = new Dialog(YourActivity.this); 
customDialog.setContentView(R.layout.dialoglayout); 
TextView text = (TextView) customDialog.findViewById(R.id.text); 
text.setText("Hello, this is a custom dialog!"); 
ImageView image = (ImageView) customDialog.findViewById(R.id.image); 
image.setImageResource(R.drawable.icon); 

customDialog.show(); 

你可以看到Android開發指南中這樣的例子: http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog