点击ListView项目Android打开自定义对话框?

时间:2013-08-14 11:15:29

标签: android android-layout android-listview

我正在尝试打开一个覆盖设备完整界面的自定义对话框。但我不知道它是抛出异常请检查我的代码并帮我解决这个问题。

点击列表视图项

data.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                passengerInformationPopup();

              }
    });

**The Function to open the Dialog**



public void passengerInformationPopup() {
    final Dialog dialog= new Dialog(getBaseContext());
    dialog.setContentView(R.layout.passenger_details_dialog); 
    String[] tittlearray ={"Mr.","Mrs.","Ms"};
    Spinner tittleSpinner = (Spinner) dialog.findViewById(R.id.Tittle);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, tittlearray); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    tittleSpinner.setAdapter(dataAdapter);
    dialog.show();
}

异常

08-14 16:39:50.338: E/AndroidRuntime(30294): FATAL EXCEPTION: main
08-14 16:39:50.338: E/AndroidRuntime(30294): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:656)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:328)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:151)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.app.Dialog.show(Dialog.java:277)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.example.userdetails.MainActivity.passengerInformationPopup(MainActivity.java:181)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.example.userdetails.MainActivity$5.onItemClick(MainActivity.java:149)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AbsListView.performItemClick(AbsListView.java:1114)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2937)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AbsListView$1.run(AbsListView.java:3695)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.os.Handler.handleCallback(Handler.java:615)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.os.Looper.loop(Looper.java:153)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.app.ActivityThread.main(ActivityThread.java:4987)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at java.lang.reflect.Method.invoke(Method.java:511)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

使用

final Dialog dialog= new Dialog(getApplicationcontext()); OR  final Dialog dialog= new Dialog(YOUR_ACTIVITY.this);

而不是

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

答案 1 :(得分:1)

使用活动上下文

     final Dialog dialog= new Dialog(ActivityName.this);

知道何时使用活动上下文

When to call activity context OR application context?

来自评论

How to set dialog to show with full screen?

编辑:

来自Dianne Hackborn的建议

为其构造函数指定一个非对话框主题,例如android.R.style.Themeandroid.R.style.Theme_Light

看看主题

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

更多信息@

https://groups.google.com/forum/#!topic/android-developers/NDFo9pF8sHY

   Dialog dialog=new Dialog(ActivityName.this,android.R.style.Theme);

答案 2 :(得分:0)

试试这个

final Dialog dialog= new Dialog(YOUR_ACTIVITY.this);

而不是

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