2014-03-04 64 views
0

我創建了自定義對話框佈局。調用時,對話框彈出正常。但是,當我嘗試使用下面評論的兩行設置對話框中的內容時,它會引發非法狀態異常。非法狀態異常:自定義對話框

private void showCustomDialog(String content){ 
    final Dialog d = new Dialog(this); 
    d.setContentView(R.layout.spread_info_popup); 
    d.show(); 

    // TextView contentTv = (TextView) findViewById(R.id.my_dialog_content); 
    // contentTv.setText(content); 

    } 

這裏有什麼問題?

我是否需要在版面spread_info_popup之前充氣,然後才能參考其中的TextView,其ID爲my_dialog_content

堆棧跟蹤:

03-04 15:03:54.690: E/AndroidRuntime(4194): FATAL EXCEPTION: main 
03-04 15:03:54.690: E/AndroidRuntime(4194): java.lang.IllegalStateException: Could not execute method of the activity 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.view.View$1.onClick(View.java:3591) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.view.View.performClick(View.java:4084) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.view.View$PerformClick.run(View.java:16966) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.os.Handler.handleCallback(Handler.java:615) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.os.Looper.loop(Looper.java:137) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at dalvik.system.NativeStart.main(Native Method) 
03-04 15:03:54.690: E/AndroidRuntime(4194): Caused by: java.lang.reflect.InvocationTargetException 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at android.view.View$1.onClick(View.java:3586) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  ... 11 more 
03-04 15:03:54.690: E/AndroidRuntime(4194): Caused by: java.lang.NullPointerException 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at com.me.reader.ui.activity.CompleteList.showSpreadInfoPopup(CompleteList.java:122) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at com.me.reader.ui.activity.CompleteList.setSpreadDescriptionfromXML(CompleteList.java:104) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  at com.me.reader.ui.activity.CompleteList.onAboutIconClicked(CompleteList.java:81) 
03-04 15:03:54.690: E/AndroidRuntime(4194):  ... 14 more 
+0

你需要做的是發佈stacktrace – tyczj

回答

2

你應該尋找的對話框裏面的觀點:

TextView contentTv = (TextView) dialog.findViewById(R.id.my_dialog_content); 
    contentTv.setText(content); 

here的文檔。

+0

如果它不是'NullPointerException'? – Raghunandan

+0

沒有必要讀取Stacktrace。這是一個NPE肯定 – Blackbelt

+0

是的,但op說「引發非法狀態異常」 – Raghunandan