非法状态异常:自定义对话框

时间:2014-03-04 14:59:39

标签: android

我创建了一个自定义对话框布局。调用时,对话框弹出正常。但是当我尝试使用下面注释的两行设置对话框内的内容时,它会抛出非法状态异常。

 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之前,我是否需要对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 进行充气?

Stacktrace:

{{1}}

1 个答案:

答案 0 :(得分:2)

您应该在对话框中查找视图:

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

here文档。