【Android】An activity without a UI must call finish() before onResume() complete

来源:互联网 发布:mac磁盘被锁定 编辑:程序博客网 时间:2024/05/04 04:31

今天遇到这样的一个bug,我的测试机是Android 7.0,经过追踪得到一下关键出错信息:

An activity without a UI must call finish() before onResume() completes

很明显这是因为当前的activity没有UI造成的,通过查看源码发现这个问题在6.0之前不存在,在6.0之后google对着快做了强制要求,下面是

Android 6.0相应的源码:

final void performResume() {    performRestart();    mFragments.execPendingActions();    mLastNonConfigurationInstances = null;    mCalled = false;    // mResumed is set by the instrumentation    mInstrumentation.callActivityOnResume(this);    if (!mCalled) {       throw new SuperNotCalledException(           "Activity " + mComponent.toShortString() +           " did not call through to super.onResume()");    }    // invisible activities must be finished before onResume() completes    if (!mVisibleFromClient && !mFinished) {        Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");        if (getApplicationInfo().targetSdkVersion                > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {            throw new IllegalStateException(                   "Activity " + mComponent.toShortString() +                   " did not call finish() prior to onResume() completing");       }   }   // Now really resume, and install the current status bar and menu.   mCalled = false;   mFragments.dispatchResume();   mFragments.execPendingActions();   onPostResume();   if (!mCalled) {       throw new SuperNotCalledException(           "Activity " + mComponent.toShortString() +           " did not call through to super.onPostResume()");    }}

16行就是打出的出错日志

知道为什么出错后解决这个问题就不难了,既然我的activity是没有UI的,出错的activity主题的配置如下:

<activity android:name=".DialogActivity"     ...    android:theme="@android:style/Theme.NoDisplay/>

这个一个没有UI的主题,为了解决问题,只需要将主题改成透明的即可,ps:记住透明不等于没有UI哦,如下所示:

<activity android:name=".DialogActivity"     ...    android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
0 0