gpt4 book ai didi

java - fragment 中的对话框使 Android 应用程序崩溃。安卓.view.WindowsManager$BadTokenException

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:10 25 4
gpt4 key购买 nike

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

这是我的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

最佳答案

更改以下行:

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

到:

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

对话框不能用应用程序上下文实例化,因为两者有不同的生命周期:对话框的生命周期绑定(bind)到宿主 Activity 的生命周期。在 SO 上进行简单搜索也可能会出现这个问题。

为了解释不同的 Context 风格以及何时(不)使用哪一个,我建议通读 possible mobile's blog post关于这个话题。它还包含一个便于引用的表格。

关于java - fragment 中的对话框使 Android 应用程序崩溃。安卓.view.WindowsManager$BadTokenException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17499602/

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