AlertDialog.show()使我的应用程序崩溃

Art*_*Rey 6 crash android android-alertdialog

这是我的问题:

我正在尝试显示AlertDialog,但我似乎无法做到这一点.

这是我的代码:

tv.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            final EditText input = new EditText(c);

            AlertDialog.Builder adb = new AlertDialog.Builder(c);
            adb.setTitle(lb)
            .setMessage("Test")
            .setView(input)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    //tv.setText(input.getEditableText().toString());
                    Toast.makeText(c, input.getEditableText().toString(), Toast.LENGTH_LONG).show(); 
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                  public void onClick(DialogInterface dialog, int whichButton)
                  {
                      dialog.cancel();
                  }
            }).show();
        }
    });
Run Code Online (Sandbox Code Playgroud)

我想在用户点击标签时显示此AlertDialog,然后在用户按OK时使用编辑文本更改标签值.

但是当它在点击时显示对话框时,它会崩溃.

07-18 16:04:59.240: E/AndroidRuntime(10503): FATAL EXCEPTION: main
07-18 16:04:59.240: E/AndroidRuntime(10503):    android.view.WindowManager$BadTokenException: Unable to add window -- 
token null is not for an application
07-18 16:04:59.240: E/AndroidRuntime(10503):    at     android.view.ViewRootImpl.setView(ViewRootImpl.java:710)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.app.Dialog.show(Dialog.java:277)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at com.technicachat.webdatadomo.Consignes$2$1.run(Consignes.java:114)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.app.Activity.runOnUiThread(Activity.java:4784)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at com.technicachat.webdatadomo.Consignes$2.onClick(Consignes.java:90)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.view.View.performClick(View.java:4211)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.view.View$PerformClick.run(View.java:17267)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.os.Handler.handleCallback(Handler.java:615)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.os.Looper.loop(Looper.java:137)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-18 16:04:59.240: E/AndroidRuntime(10503):    at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

在收到此消息之前,线路较少:

07-18 16:04:56.645: I/Choreographer(10503): Skipped 32 frames!  The application may be doing too much work on its main thread.
Run Code Online (Sandbox Code Playgroud)

我知道这意味着什么,但我只是在展示一个对话框,这不是很多工作!

我希望你们能帮助我!

再见

Ion*_*ers 26

你的变量c应该是YourActivity.this而不是getApplicationContext()


Ram*_*yer 5

您需要考虑几件事。

  1. 您正在自定义警报对话框,例如定位按钮和设置边距等布局参数。

    如果您这样做,使用 V7 支持的警报对话框将解决问题。确保您已在项目中导入v7 支持库。

    更改android.app.AlertDialog.Builderandroid.support.v7.app.AlertDialog.Builder

  2. 您正在活动或活动的子类中或在片段使用中创建警报对话框

    始终传递活动上下文而不是 baseContextapplicationContext

    传递错误的上下文(例如 applicationContext 或 baseContext)将导致 WindowManager-BadToken 异常

在活动...

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)

在活动的子类中...

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
Run Code Online (Sandbox Code Playgroud)

在一个片段...

AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); // Activity inherits from Context and hence will work.
Run Code Online (Sandbox Code Playgroud)

我发现,对警报对话框的大量自定义与 v7 Support AlertDialog 类配合得很好。

就我而言,如果有多个按钮,我必须将警报对话框的按钮居中并设置左右边距。将导入更改为 v7 支持解决了该问题。

希望这可以帮助。