1

I am getting an error saying that

java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at com.myapp.contactList.MainPage.onCreate(MainPage.java:41)

And I looked up several stack overflow questions similar to this one and tried all the solutions but still having this issue. Below is my code:

MainPage.java

public class MainPage extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_page);

    android.support.v7.app.ActionBar ab = getSupportActionBar();
    assert ab != null;
    ab.setDisplayHomeAsUpEnabled(true);  // I'M GETTING THE ERROR HERE AT THIS LINE
    ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#51c2f2")));
    ab.setDisplayUseLogoEnabled(true);

AndroidManifest.xml

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

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainPage"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>
</manifest>

styles.xml

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

activity_main_page.xml

<?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:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fitsSystemWindows="true"
   tools:context="com.myapp.contactList.MainPage">

   <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
   </android.support.design.widget.AppBarLayout>
   <include layout="@layout/content_main_page" />
</android.support.design.widget.CoordinatorLayout>
5
  • Are you sure about the theme you are using? Error says to "Use AppCompat theme" But in the code posted you are using app compat theme Commented Jul 1, 2016 at 15:03
  • I am not sure if I understood your comment. Error tells me to use AppCompat theme and I am using AppCompat theme, so why is that error being thrown? Commented Jul 1, 2016 at 15:13
  • @someonenew check my answer Commented Jul 1, 2016 at 15:16
  • is this the same style you are using? Commented Jul 1, 2016 at 15:19
  • @someonenew did you try my solution? Commented Jul 1, 2016 at 15:30

2 Answers 2

1

Basically you have two errors in your code :

First, assert keyword is not supported by Android, when you use it in your code just nothing happen. You should check that this code :

if(ab  ==  null) {
   throw new IllegalStateException();
}

or you can also use

Assert.notNull(ab);

from some lib or framework.

You can also switch on assert but only for your device via adb:

adb shell setprop debug.assert 1

Second you should use Toolbar as support action bar. Update your MainActivity class

public class MainPage extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        android.support.v7.widget.Toolbar toolbar 
             = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar)

        setSupportActionBar(toolbar)
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);  
        ...

And update layout xml as is shown in the code below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    ....
   tools:context="com.myapp.contactList.MainPage">

   <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:theme="?attr/actionBarTheme"
        android:minHeight="?attr/actionBarSize">

   </android.support.design.widget.AppBarLayout>
   <include layout="@layout/content_main_page" />
</android.support.design.widget.CoordinatorLayout>

You have to also set style "@style/AppTheme.NoActionBar" for your activity

You can define it in style.xml as is shown below:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Sign up to request clarification or add additional context in comments.

5 Comments

I added that statment and I get this error Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. So I know it's telling me to change theme to AppCompat but I already am using AppCompat, what should I change?
you have to call setSupportActionBar(Toolbar) before you call getSupportActionBar(). Otherwise you always will get null
put toolbar in xml and in <android.support.design.widget.AppBarLayout> <android.support.v7.widget.Toolbar .../> </android.support.design.widget.AppBarLayout>
That worked nicely, thank you. Now how do I change the color of the toolbar and change title name. I tried toolbar.setTitle("List"); but it didn't do anything.
use setTitle("List) from activity
1

I think you need to use setSupportActionBar to set it first. something like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

this layout:

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

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

and this style in manifest:

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

which removes actionBar:

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

I guess you are trying to get something that is not a SupportActionBar, so this happens.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.