Reputation: 1322
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>
Upvotes: 1
Views: 682
Reputation: 12365
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>
Upvotes: 1
Reputation: 9173
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.
Upvotes: 1