【问题标题】:Logcat throws errors when creating a Dialog "token Null is not for an application"Logcat 在创建对话框“token Null 不适用于应用程序”时抛出错误
【发布时间】:2014-05-24 20:20:04
【问题描述】:

根据logcat 的输出,应用程序在创建Dialog 时崩溃。我尝试将变量reportAlertDialog 更改为AlertDialog 类型,但我发现某些功能/属性不可用,例如setcontentView,但当变量为Dialog 类型时它可用。无论如何,此时应用程序崩溃,logcat 输出相当莫名其妙。有什么建议吗?

Java代码:

private void report(Bundle thisActivityBundle) {
            ...
                            ...
            if ( (reportAlertDialog != null) && (reportAlertDialog.isShowing()) ) {
                reportAlertDialog.cancel();
                Log.i(CURRENT_ACTIVITY, "@report(): reportAlertDialogue Was Showing And Now Is Canceled");
            }

            String sortedKeys[] = {LOC_NAME_KEY, LOC_LAT_KEY, LOC_LNG_KEY, LOC_TIME_KEY, LOC_DATE_KEY, IMG_TITLE_KEY, IMG_PATH_KEY};
            String bundleVals [] = new String[sortedKeys.length];

            for (int i=0; i<sortedKeys.length; i++) {
                if (thisActivityBundle.containsKey(sortedKeys[i])) {
                    bundleVals[i] = thisActivityBundle.getString(sortedKeys[0]).toString();
                }else {
                    bundleVals[i] = "NULL";
                }
            }

            reportAlertDialog = new Dialog(getApplicationContext());
            LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
            View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.report_dialog, null);

            reportAlertDialog.setContentView(reportAlertDialogInflatedView);

            int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
                    R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
            };
            TextView [] viewsVars = new TextView[viewsRefsIds.length];

            TextView reportAlertDialogMSG = (TextView) reportAlertDialog.findViewById(R.id.reportDialogMessageID);
            reportAlertDialogMSG.setText(REPORT_ALERT_DIALOG_MSG);

            for (int i=0; i<bundleVals.length; i++) {
                viewsVars[i] = (TextView) reportAlertDialog.findViewById(viewsRefsIds[i]);
                viewsVars[i].setText(bundleVals[i]);
            }

            reportAlertDialog.show();

        }

Logcat:

05-24 08:06:01.317: E/AndroidRuntime(2396): FATAL EXCEPTION: main

05-24 08:06:01.317:E/AndroidRuntime(2396):进程:com.example.meetingpointlocator_03,PID:2396 05-24 08:06:01.317: E/AndroidRuntime(2396): android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:540) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.app.Dialog.show(Dialog.java:286) 05-24 08:06:01.317: E/AndroidRuntime(2396): at com.example.meetingpointlocator_03.MeetingPointFix$4.report(MeetingPointFix.java:307) 05-24 08:06:01.317: E/AndroidRuntime(2396): at com.example.meetingpointlocator_03.MeetingPointFix$4.onClick(MeetingPointFix.java:240) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.os.Handler.dispatchMessage(Handler.java:102) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.os.Looper.loop(Looper.java:136) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 android.app.ActivityThread.main(ActivityThread.java:5017) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 java.lang.reflect.Method.invoke(Method.java:515) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 05-24 08:06:01.317: E/AndroidRuntime(2396): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-24 08:06:01.317: E/AndroidRuntime(2396): at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: android dialog android-alertdialog


    【解决方案1】:

    尝试将对话框的参数设置为此活动

        reportAlertDialog = new Dialog(YourActivityName.this);
    

    我认为它会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多