活动没叫完?(API 23)

dak*_*aka 31 android runtime-error android-lifecycle android-6.0-marshmallow

我收到以下错误,我不知道为什么会发生这种情况.

错误:

08-23 17:07:46.533  22454-22454/com.a.b.c E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.a.b.c, PID: 22454
    java.lang.RuntimeException: Unable to resume activity {com.a.b.c/com.a.b.c.MainActivity}: java.lang.IllegalStateException: Activity {com.a.b.c/com.a.b.c.MainActivity} did not call finish() prior to onResume() completing
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Activity {com.a.b.c/com.a.b.c.MainActivity} did not call finish() prior to onResume() completing
            at android.app.Activity.performResume(Activity.java:6324)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Run Code Online (Sandbox Code Playgroud)

码:

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    System.out.println("Started");
}
Run Code Online (Sandbox Code Playgroud)

我试图在运行Android 6.0(API 23)的AVD上运行代码,适用于API 22.

Ale*_*ang 20

我有同样的问题,同样的崩溃

did not call finish() prior to onResume() completing 
Run Code Online (Sandbox Code Playgroud)

错误信息.所以我创建了v23\styles.xml

<style name="AppTheme" parent="android:Theme.Translucent">
...
</style>
Run Code Online (Sandbox Code Playgroud)

而普通的styles.xml有

<style name="AppTheme" parent="android:Theme.NoDisplay">
...
</style>
Run Code Online (Sandbox Code Playgroud)

它工作正常,不再崩溃.但是,我不知道这个解决方案有多好,在API 23中使用Theme.Translucent,特别是因为它被定义

半透明活动的主题(API级别10及更低级别).

我真的希望他们能解决这个问题.

  • 我推荐`Theme.Translucent.NoTitleBar`.否则,您将在屏幕上浮动一个旧式标题栏.否则,这种技术对我有用 - 非常感谢! (12认同)
  • 看起来这不是一个bug.http://developer.android.com/reference/android/R.attr.html#windowNoDisplay上的文档说"活动必须立即退出而不等待用户交互".因此异常消息表明Activity需要在`onResume()`完成之前完成. (2认同)

Sam*_*Sam 18

我找到了一个解决方法.通话setVisible(true)onStart():

@Override
protected void onStart() {
    super.onStart();
    setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)

  • 我很抱歉.我想我试过`setVisible(false)`.`setVisible(true)`避免了崩溃,但最终显示出无用的活动UI(黑色背景,旧式灰色标题栏).`Theme.Translucent.NoTitleBar`更接近于'Theme.NoDisplay`的视觉效果(即不可见). (2认同)

Mal*_*ngh 15

这是Android M开发人员预览中的错误.更多细节

  • 1.断开的链接2.从api 23开始,这似乎是行为上的预期变化,而不是错误,下面的答案可能会有所帮助,并且这篇文章对其进行了总结:https://commonsware.com/blog/2015/11/ 02 / psa-android-6p0-theme.nodisplay-regression.html (2认同)

Rid*_*lly 5

我的隐形活动显示了一个确认对话框。这确实失去了材料的设计外观,当我使用android:Theme.Translucent.NoTitleBar

因此,基于上面的答案和 CommonWare 的博客以及 Android 主题定义,我使用了这种样式,它扩展了一个普通的 AppCompat.Light 主题:

<style name="AppTheme.NoDisplay" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowNoTitle">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Two*_*ter 5

我已经找到了实际的解决方法。该Theme.NoDisplay仅对

不实际显示 UI 的活动;也就是说,他们在恢复之前完成了自己。

来源:Android 开发

因此,如果您分配这个主题的活动中,你必须调用finish()退出之前onCreate()(系统调用之前即onResume)这就是为什么错误说“没有调用完成()之前的onResume()”。

因此,该主题的实际用例是引导程序活动等,例如,当您必须根据用户是否已通过身份验证来确定是显示登录视图还是主视图时:

public class BootstrapActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!userLoggedIn) {
            startActivity(new Intent(this, LoginActivity.class));
        } else {
            startActivity(new Intent(this, MainActivity.class));
        }
        finish();
    }

}
Run Code Online (Sandbox Code Playgroud)