3

我试图创建一个对话框,但是当我尝试运行代码时它崩溃了。我想在我的片段中的列表视图中选择一个项目时运行它,现在我只想尝试显示对话框而不传递任何内容

这是我的java代码:

        myInfoList.setAdapter(new CustomAdapter(details , getActivity().getApplicationContext()));

        myInfoList.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                   //System.out.println("Name: "+details.get(position).getSub();
                   //String s = details.get(position).getDescription();
                   final Dialog dialog = new Dialog(getActivity().getApplicationContext());
                   dialog.setContentView(R.layout.purchase);
                   dialog.setTitle("Title");

                   Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();
               }
       });

日志猫:

07-06 12:05:16.012: V/21st Polling:(12319): clicked
07-06 12:05:16.957: D/AndroidRuntime(12319): Shutting down VM
07-06 12:05:16.957: W/dalvikvm(12319): threadid=1: thread exiting with uncaught exception (group=0x40e20930)
07-06 12:05:16.972: E/AndroidRuntime(12319): FATAL EXCEPTION: main
07-06 12:05:16.972: E/AndroidRuntime(12319): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.app.Dialog.show(Dialog.java:281)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at com.example.isellcrazyitem.SalesOrder$1.onItemClick(SalesOrder.java:104)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AbsListView.performItemClick(AbsListView.java:1102)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2751)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AbsListView$1.run(AbsListView.java:3426)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.os.Handler.handleCallback(Handler.java:725)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.os.Looper.loop(Looper.java:137)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.app.ActivityThread.main(ActivityThread.java:5233)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at dalvik.system.NativeStart.main(Native Method)
07-06 12:05:18.613: I/Process(12319): Sending signal. PID: 12319 SIG: 9
4

2 回答 2

9

更改以下行:

final Dialog dialog = new Dialog(getActivity().getApplicationContext());

至:

final Dialog dialog = new Dialog(getActivity());

不能使用应用程序上下文实例化对话框,因为两者具有不同的生命周期:对话框的生命周期与宿主的生命周期绑定Activity。在这里对 SO 进行简单的搜索可能也会提出这一点。

有关不同Context风味以及何时(不)使用哪种风味的解释,我建议您阅读有关该主题的可能的移动博客文章。它还包含一个方便的表格供参考。

于 2013-07-06T04:28:09.410 回答
5

改变这个

final Dialog dialog = new Dialog(getActivity());

而不是这个

final Dialog dialog = new Dialog(getActivity().getApplicationContext());

&当你传递上下文时要记住。

  1. 在片段中使用getActivity()
  2. 活动中YourActivity.this

原因是您无法通过不是 Activity 的 Context 显示应用程序窗口/对话框。尝试传递有效的活动参考。

于 2013-07-06T04:30:50.990 回答