I started a project in Android Studio and apparently the Theme was Theme.NameOfProject.NoActionBar.
This is causing a the following error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weoutdoor/com.example.weoutdoor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
The project themes.xml :
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WeOutdoor" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.WeOutdoor.NoActionBar">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.WeOutdoor.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.WeOutdoor.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
And the AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.weoutdoor">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WeOutdoor.AppBarOverlay"
tools:targetApi="31">
<activity
android:name=".RegisterActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.WeOutdoor.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UserProfileActivity"
android:exported="true"
android:theme="@style/Theme.WeOutdoor.AppBarOverlay">
</activity>
<activity android:name=".LoginActivity"
android:exported="true"
android:theme="@style/Theme.WeOutdoor.AppBarOverlay">
</activity>
</application>
I tried to change style parent and theme to : ThemeOverlay.AppCompat.Dark.ActionBar
but every time i try to use the code getSupportActionBar().setTitle("WeOutdoor");
i get the mentioned error above.
CodePudding user response:
You're trying to use a ThemeOverlay
, which isn't the same as a full Theme
. But, honestly, just stick with the NoActionBar
theme and create your own toolbar at the top. It's just a layout containing a TextView
for the title and some ImageButton
s for icons.
CodePudding user response:
This problem might be caused by your theme. Check it again, and make sure that it parents with the Theme.AppCompat.Light.DarkActionBar.
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">true</item>
...
</style>
If your activity extends AppCompatActivity
or ActionBarActivity
, call getSupportActionBar()
.
I had the null pointer issue using getSupportActionBar()
. My issue was fixed by adding <item name="windowActionBar">true</item>
in my styles.xml