gpt4 book ai didi

android - 单击 ListView 项目 Android 打开自定义对话框?

转载 作者:行者123 更新时间:2023-11-29 21:36:56 25 4
gpt4 key购买 nike

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

点击 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)

最佳答案

使用 Activity 上下文

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

知道何时使用 Activity 上下文

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);

关于android - 单击 ListView 项目 Android 打开自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com