java.lang.IllegalStateException:ActionBarImpl 只能与兼容的窗口装饰布局一起使用

发布于 2024-12-03 21:04:19 字数 5343 浏览 2 评论 0原文

在过去的几天里,我确实看到越来越多的错误消息听起来相同。我不使用平板电脑 ActionBar - 我只为手机开发。

这些崩溃的原因是什么?

这是一个完整的跟踪:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=8995, result=0, data=null} to activity {xy.yyyy.app.android/xy.yyyy.app.Main}: java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout
at android.app.ActivityThread.deliverResults(ActivityThread.java:2818)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2861)
at android.app.ActivityThread.access$1000(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout
at com.android.internal.app.ActionBarImpl.init(ActionBarImpl.java:214)
at com.android.internal.app.ActionBarImpl.<init>(ActionBarImpl.java:200)
at android.app.Dialog.show(Dialog.java:255)
at xy.yyyy.app.MyProgressDialog.show(MyProgressDialog.java:33)
at xy.yyyy.app.MyProgressDialog.show(MyProgressDialog.java:23)
at xy.yyyy.app.Main$FetchTask.onPreExecute(Main.java:19)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
at android.os.AsyncTask.execute(AsyncTask.java:499)
at xy.yyyy.app.Main.onActivityResult(Main.java:60)
at android.app.Activity.dispatchActivityResult(Activity.java:4581)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2814)
... 11 more

这是一个典型的 AsyncTask,我在活动中使用它来使用 ProgressDialog 启动一项冗长的任务:

private class DeleteTask extends AsyncTask<Void, Void, Boolean> {

  @Override
  protected void onPreExecute () {
    if (!isRunning) {
      progressDialog = MyProgressDialog.show(MyActivity.this,
                                             null,
                                             null,
                                             true,
                                             false);
    }
  }

  @Override
  protected Boolean doInBackground(final Void... strings) {
    Boolean rc = false;

    if (!isRunning) {
      isRunning = true;

      processDelete();
    }

    return rc;
  }

  @Override
  protected void onPostExecute(final Boolean result) {
    if (progressDialog != null) {
      progressDialog.cancel();
    }
    progressDialog = null;

    setResult(RESULT_OK);
    finish();

    isRunning = false;
  }
}

这是我的 MyProgressDialog 类:

public class MyProgressDialog extends Dialog {

  public MyProgressDialog(final Context context) {
    super(context, R.style.MyProgressDialog);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message) {
    return show(context, title, message, false);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message, final boolean indeterminate) {
    return show(context, title, message, indeterminate, false, null);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message, final boolean indeterminate, final boolean cancelable) {
    return show(context, title, message, indeterminate, cancelable, null);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message, final boolean indeterminate, final boolean cancelable, final OnCancelListener onCancelListener) {
    MyProgressDialog dialog = new MyProgressDialog(context);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(onCancelListener);
    dialog.setTitle(title);

    dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    dialog.show();

  return dialog;
  }
}

提前非常感谢。

编辑:

这是使用的样式:

<style name="MyProgressDialog">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowTitleStyle">@null</item>
</style>

这是 ProgressDialog 的屏幕截图:

在此处输入图像描述

<强>解决方案:

必须将其添加到样式中:

parent="@android:style/Theme.Dialog"

Within the last days I do see increasing error messages that sound identical. I don't use tablets ActionBar - I'm developing for phones only.

What's the reason for these crashes?

Here's a complete trace:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=8995, result=0, data=null} to activity {xy.yyyy.app.android/xy.yyyy.app.Main}: java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout
at android.app.ActivityThread.deliverResults(ActivityThread.java:2818)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2861)
at android.app.ActivityThread.access$1000(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout
at com.android.internal.app.ActionBarImpl.init(ActionBarImpl.java:214)
at com.android.internal.app.ActionBarImpl.<init>(ActionBarImpl.java:200)
at android.app.Dialog.show(Dialog.java:255)
at xy.yyyy.app.MyProgressDialog.show(MyProgressDialog.java:33)
at xy.yyyy.app.MyProgressDialog.show(MyProgressDialog.java:23)
at xy.yyyy.app.Main$FetchTask.onPreExecute(Main.java:19)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
at android.os.AsyncTask.execute(AsyncTask.java:499)
at xy.yyyy.app.Main.onActivityResult(Main.java:60)
at android.app.Activity.dispatchActivityResult(Activity.java:4581)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2814)
... 11 more

This is a typical AsyncTask that I use from within an activity to start a lengthy task with a ProgressDialog:

private class DeleteTask extends AsyncTask<Void, Void, Boolean> {

  @Override
  protected void onPreExecute () {
    if (!isRunning) {
      progressDialog = MyProgressDialog.show(MyActivity.this,
                                             null,
                                             null,
                                             true,
                                             false);
    }
  }

  @Override
  protected Boolean doInBackground(final Void... strings) {
    Boolean rc = false;

    if (!isRunning) {
      isRunning = true;

      processDelete();
    }

    return rc;
  }

  @Override
  protected void onPostExecute(final Boolean result) {
    if (progressDialog != null) {
      progressDialog.cancel();
    }
    progressDialog = null;

    setResult(RESULT_OK);
    finish();

    isRunning = false;
  }
}

Here's my MyProgressDialog class:

public class MyProgressDialog extends Dialog {

  public MyProgressDialog(final Context context) {
    super(context, R.style.MyProgressDialog);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message) {
    return show(context, title, message, false);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message, final boolean indeterminate) {
    return show(context, title, message, indeterminate, false, null);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message, final boolean indeterminate, final boolean cancelable) {
    return show(context, title, message, indeterminate, cancelable, null);
  }

  public static MyProgressDialog show(final Context context, final CharSequence title, final CharSequence message, final boolean indeterminate, final boolean cancelable, final OnCancelListener onCancelListener) {
    MyProgressDialog dialog = new MyProgressDialog(context);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(onCancelListener);
    dialog.setTitle(title);

    dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    dialog.show();

  return dialog;
  }
}

Many thanks in advance.

EDIT:

Here's the used style:

<style name="MyProgressDialog">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowTitleStyle">@null</item>
</style>

Here's a screenshot of that ProgressDialog:

enter image description here

SOLUTION:

Had to add this to the style:

parent="@android:style/Theme.Dialog"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文