Script Kitty
Script Kitty

Reputation: 1747

Android Crash on start: Styling the ActionBar Theme App.Compat

I don't want to post since it seems simple. For the sake of simplicity I'll provide as much detail as I can without throwing up a bunch of logcat and expecting a cure all.

Following the google tutorial for styling an action bar. Win7, Android Studio, Android 5, API 19 KitKat (Min SDK Version 11) no support library, Gradle 1.8 I think.

MainActivity.java excerpt:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //ERRORS: // super.onCreate(savedInstanceState);
        //ERRORS: // setContentView(R.layout.activity_main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_search:
                // openSearch();
                return true;
            case R.id.action_settings:
                // openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

LogCat excerpt(when SuperNotCalled):

Process: gaga.june, PID: 8726
    android.util.SuperNotCalledException: Activity {gaga.june/gaga.june.MainActivity} did not call through to super.onCreate()

LogCat excerpt (when I put in the Super):

Unable to start activity ComponentInfo{gaga.june/gaga.june.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

AndroidManifest.xml excerpt:

    <uses-sdk android:minSdkVersion="11"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:parentActivityName="gaga.june.MainActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="gaga.june.MainActivity" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

WHAT I'VE TRIED:

  • setContentView(R.layout.activity_main); Also gives me an error when debugging. Checked the manual, they said that you must define the ListView if it gives you an error like that. But I'm not using a listview, I only defined TextView Therefore, I commented it out

  • I changed my style from <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> to <style name="AppTheme" parent="Theme.AppCompat.Light">

  • Other than that I pretty much followed the tutorial exactly to my project. If I could just identify what all these problems mean that would be great (SO is always a last resort I may just skip this tutorial). Thanks so much

    EDIT CustomActionBarTheme from themes.xml:

    <!-- Theme applied to app/activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    

    Upvotes: 0

    Views: 1536

    Answers (3)

    dovahkiin
    dovahkiin

    Reputation: 43

    The reason you are having this problem is because the activity you are trying to apply the theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.

    Change the parent of the actual java code to be just plain Activity and you should be able to leave the theme on it.

    Upvotes: 0

    Gabriele Mariotti
    Gabriele Mariotti

    Reputation: 363667

    First of all in your onCreate method:

    • you have to call the super method
    • you have to define your layout with setContentView method
    • you have to use the getSupportActionBar() instead of getActionBar method

    Something like:

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

    Then you have to change the style CustomActionBarTheme used by your MainActivity, because the ActionBarActivity requires an AppCompat theme

    <!-- Theme applied to app/activity -->
    <style name="CustomActionBarTheme"
        parent="Theme.AppCompat.Light">
           ......
    </style>
    

    Finally I suggest you switching to the new app-compat v 22.2.0 changing your build.gradle

    dependencies {
         compile 'com.android.support:appcompat-v7:22.2.0'
    }
    

    With this version the ActionBarActivity is deprecated. You can use now the AppCompatActivity

    Upvotes: 3

    kandroidj
    kandroidj

    Reputation: 13922

    For Theme.AppCompat you need to include the support library in your gradle.build file:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.2.0'
    }
    

    Additionally ActionBarActivity is deprecated, use AppCompatActivity

    Upvotes: 2

    Related Questions