-2

I am having the most frustrating issue with a new Activity. I created it in the "normal" way:

Right-Click -> New -> Activity -> Empty Activity

However I am getting this annoying error when I try to launch the activity:

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

I feel my question is different from the others on StackOverflow because I've done some poking around on StackOverflow as well as googling in general, but these errors seem to pop up when somebody tries to open a dialog box, not when they are simply trying to launch a new activity.

Here is my Activity Class ViewAllStudents.java:

public class ViewAllStudents extends AppCompatActivity {

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

I am getting the error on the setContentView(R.layout.activity_view_all_students); line.

My layout resource file activity_view_all_students.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_all_students_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/student_table_header" />

    <ListView
        android:id="@+id/student_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

My include file student_table_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/DARKOLIVEGREEN"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/student_number_header"
        style="@style/StdTableHeader"
        android:layout_weight="1"
        android:text="@string/student_number_header"/>

    <TextView
        android:id="@+id/student_id_header"
        style="@style/StdTableHeader"
        android:text="@string/student_id_header"/>

    <TextView
        android:id="@+id/student_location_header"
        style="@style/StdTableHeader"
        android:text="@string/student_location_header"/>

    <TextView
        android:id="@+id/student_status_header"
        style="@style/StdTableHeader"
        android:text="@string/student_status_header"/>

    <TextView
        android:id="@+id/student_delete_header"
        style="@style/StdTableHeader"
        android:text="@string/student_injuries_header"/>

    <TextView
        android:id="@+id/student_deleted_header"
        style="@style/StdTableHeader"
        android:text="@string/student_deleted_header"/>

    <TextView
        android:id="@+id/student_last_modified"
        style="@style/StdTableHeader"
        android:text="@string/student_last_modified_header"/>

</LinearLayout>

Finally, my styles resource file styles.xml:

<resources>
    <style name="StdTableHeader">
        <item name="android:textSize">14sp</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:paddingRight">4dp</item>
        <item name="android:paddingTop">2dp</item>
        <item name="android:paddingBottom">2dp</item>
        <item name="android:layout_weight">10</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/WHITE</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textAllCaps">true</item>
        <item name="android:gravity">center</item>
    </style>
</resources>

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <activity
            android:name=".main.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".forms.formOne"
            android:label="@string/form_one_header"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden" />
        <activity
            android:name=".forms.formTwo"
            android:label="@string/form_two_header"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden" />
        <activity
            android:name=".forms.formThree"
            android:label="@string/form_three_header"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden" />
        <activity
            android:name=".main.AddStudent"
            android:label="@string/title_activity_add_patient"
            android:theme="@style/AppTheme.NoActionBar" />

        <activity android:name=".main.ViewAllStudents"></activity>
    </application>

</manifest>
Brian
  • 1,726
  • 2
  • 24
  • 62
  • 1
    hint: `extends AppCompatActivity` – Tim Jan 09 '17 at 17:13
  • Possible duplicate of [You need to use a Theme.AppCompat theme (or descendant) with this activity](http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity) – mlg Jan 09 '17 at 17:15
  • `You need to use a Theme.AppCompat theme (or descendant) with this activity.` Do it. – Eugen Pechanec Jan 09 '17 at 17:15
  • post manifest file and style for the App – santosh kumar Jan 09 '17 at 17:17
  • @TimCastelijns Ummm ... my class does `extend AppCompatActivity` . – Brian Jan 09 '17 at 17:17
  • there is problem in style of the application. – santosh kumar Jan 09 '17 at 17:18
  • @EugenPechanec Your comment is absolutely no help. I was under the assumption that sarcasm was frowned upon here on StackOverflow. – Brian Jan 09 '17 at 17:18
  • @mlg It is not the same. Like I said in my original post, I am not opening a dialog. – Brian Jan 09 '17 at 17:19
  • Indeed, you are extending `AppCompatActivity`. The reason you need an AppCompat theme should be obvious – Tim Jan 09 '17 at 17:19
  • @TimCastelijns It is not obvious. Otherwise I would not be on StackOverflow asking for assistance. – Brian Jan 09 '17 at 17:20
  • @Brian And I was under the impression that engineers look at the error and do what it says before asking redundant questions. Post your AndroidManifest.xml. And themes.xml or styles.xml, anywhere your theme is defined. – Eugen Pechanec Jan 09 '17 at 17:20
  • **AppCompat**Activity needs an **AppCompat** theme, I dont know what else to say – Tim Jan 09 '17 at 17:24
  • @Brian post your manifest. – ashubuntu Jan 09 '17 at 17:25
  • All ... I have posted my **AndroidManifest.xml** as requested. – Brian Jan 09 '17 at 17:27
  • @TimCastelijns Every other activity in my app works. This one isn't working for some reason, and I created it the very same way as my other activities. – Brian Jan 09 '17 at 17:29
  • If you have a look at the manifest you can easily spot the difference, being that your new activity has no theme specified. – Tim Jan 09 '17 at 17:30
  • 1
    @Brian ... Theme.AppCompat theme ***(or descendant)***. `AppTheme` extends `Theme.Appcompat.*`. Your last activity doesn't specify any theme. Boom. That's why we define the default theme on `application` instead of having to think about every bloody activity we add. – Eugen Pechanec Jan 09 '17 at 17:34
  • @TimCastelijns sonofabitch ... I hadn't noticed that before, and I have no explanation as to why AndroidStudio didn't generate that line for me like it did for every other new Activity I have ever created. I didn't even think of looking at the manifest file. Thanks a bunch! – Brian Jan 09 '17 at 17:39
  • @Brian Android Studio generates `android:theme="@style/AppTheme` on the `application` element when you create a project. It doesn't handle individual `activities`. – Eugen Pechanec Jan 09 '17 at 17:41
  • Sure. Considering this is caused by a "typo", and to restore you your lost rep, you may delete the question if you don't mind – Tim Jan 09 '17 at 17:41
  • @TimCastelijns I tried deleting the question but StackOverflow won't allow me. Also, I don't really consider it a "typo" as much as a abnormality in AndroidStudio that it didn't set those lines for me upon activity generation. – Brian Jan 09 '17 at 17:44

2 Answers2

1

Add this attribute

android:theme="@style/Theme.AppCompat.Light"

in the activity tag.

Or have the styles.xml like the following

<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>

</resources>

and in manifest add this attribute

android:theme="@style/AppTheme"

in the application tag.

ashubuntu
  • 747
  • 7
  • 20
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
  • None of my other three activities have that, and they all work. – Brian Jan 09 '17 at 17:30
  • you have used MainActivity extends AppcompactActivity so you should use this – santosh kumar Jan 09 '17 at 17:33
  • @Brian Your other activites have `AppTheme` which happens to extend `Theme.AppCompat`. So your other activities don't crash. – Eugen Pechanec Jan 09 '17 at 17:35
  • @Brian Next time try what's being suggested instead of blatantly shutting off any attempt to help with `None of my other three activities have that, and they all work.`. Glad we could help. – Eugen Pechanec Jan 09 '17 at 17:38
  • @EugenPechanec My apologies. I never even thought about the manifest file as AndroidStudio has always generated the required `android:theme="@style/Theme.AppCompat.Light"` lines for me when I created a new Activity. It is so strange that it didn't happen, that I didn't think to look in the manifest. Thanks for your help. – Brian Jan 09 '17 at 17:42
  • @Brian Perhaps a change in different version of AS. Alright, mark the answer as correct and move on. See you on StackOverflow! – Eugen Pechanec Jan 09 '17 at 17:43
  • seeing as this answer doesn't explain anything at all, you really do not have to accept it. It may be better to write up your own answer seeing as you now know what the problem was – Tim Jan 09 '17 at 17:45
  • @EugenPechanec Maybe this will garner some sympathy for me. I am working on a development machine that is not connected to the internet, and will never be connected to the internet. It is quite difficult for me to get new software from this internet machine to my development machine. Also, I am the only person working on this project and have nobody to do a quick sanity check on my issues. Thanks, again, for your help. – Brian Jan 09 '17 at 17:46
0

Ok ... to make a long answer short, check your AndroidManifest.xml files. For some reason, AndroidStudio neglected to insert critical line:

android:theme="@style/AppTheme.NoActionBar"

AndroidStudio had inserted this line for all of my other activities that I have created, except for this one.

Thanks to all who took the time to help and who tolerated my ignorance.

Brian
  • 1,726
  • 2
  • 24
  • 62