App crashing after theme update

时光总嘲笑我的痴心妄想 提交于 2020-01-11 14:48:07

问题


The app I was working on so far was working fine until I added a couple of new activities and tried applied some changes to the theme.

The following is my styles.xml file. I looked into other results and added a parent to AppTheme as I was getting an error regarding AppCompat earlier. I also made AppTheme parent of NoActionBar. One thing I noticed was when I apply AppBarOverlay to Register User Activity it crashes and when I apply other it doesn't. Main Activity is crashing regardless.

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar" parent ="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

Below is the Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ati.activate">
<application
    android:allowBackup="true"
    android:icon="@drawable/footprintblue"
    android:label="@string/app_name"
    android:roundIcon="@drawable/footprintgreen"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

    <activity android:name=".UserManagement.LoginActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".UserManagement.RegisterUserActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.AppBarOverlay">
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
    </activity>

 </application>
</manifest>

I have applied AppTheme to android and then overridden the desired themes to other activities. Note: Login screen works and is using AppTheme.NoTitleBar, Register User works without AppBarOverLay theme but crashes when this theme is used.

This is my activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ati.activate.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <!--android:paddingTop="@dimen/appbar_padding_top"-->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways">
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/login_background" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

And this is the snippet of my MainActivity.Java file:

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.ati.activate.Fragments.SettingsFragment.AppSettingsFragment;
import com.ati.activate.Fragments.SettingsFragment.ProfileSettingsFragment;

public class MainActivity extends AppCompatActivity{
private AppPagerAdapter mAppPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    createMenu();
}

I have tried every solution discussed on the internet but still have not found any solution. Could anyone please help? Thanking you in advance.

Below is the error when Register User Activity is opened with AppTheme.AppBarOverlay:

    12-08 04:55:08.217 17843-17850/? E/zygote: Failed writing handshake bytes (-1 
    of 14): Broken pipe
    12-08 04:55:16.704 17843-17986/com.ati.activate E/StudioProfiler: JVMTI 
    error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
    12-08 04:55:16.706 17843-17986/com.ati.activate E/StudioProfiler: JVMTI 
    error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
    12-08 04:55:16.818 17843-17843/com.ati.activate E/AndroidRuntime: FATAL 
    EXCEPTION: main
                                                                  Process: 
    com.ati.activate, PID: 17843

    java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.ati.activate/com.ati.activate.UserManagement.RegisterUserActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                      at android.os.Looper.loop(Looper.java:164)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                   Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                      at com.ati.activate.UserManagement.RegisterUserActivity.onCreate(RegisterUserActivity.java:13)
                                                                      at android.app.Activity.performCreate(Activity.java:6975)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                      at android.os.Looper.loop(Looper.java:164) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

And this is the error when Main Activity is opened:

12-08 04:59:50.431 21418-21586/com.ati.activate E/StudioProfiler: JVMTI 

error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
12-08 04:59:50.432 21418-21586/com.ati.activate E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
12-08 04:59:50.518 21418-21418/com.ati.activate E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.ati.activate, PID: 21418
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ati.activate/com.ati.activate.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                      at android.os.Looper.loop(Looper.java:164)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                   Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                      at com.ati.activate.MainActivity.onCreate(MainActivity.java:26)
                                                                      at android.app.Activity.performCreate(Activity.java:6975)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                      at android.os.Looper.loop(Looper.java:164) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

回答1:


Put this code in your style.xml

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

and don't add parent ="AppTheme" in the above code.




回答2:


change your manifest file :

<activity android:name=".UserManagement.RegisterUserActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.AppBarOverlay">
</activity>

to this :

<activity android:name=".UserManagement.RegisterUserActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
</activity>

i think your RegisterUserActivity problem will be solve...

UPDATE for your MainActivty Chnage style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>



回答3:


It works with the following changes to the code:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>


来源:https://stackoverflow.com/questions/47707707/app-crashing-after-theme-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!