Issue
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.
After trying to apply some of the suggestion in the answers to this post i ended up with new error :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Currently my themes is as following :
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="MyTheme"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="windowActionBar">true</item>
And my AndroidMenifest.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/MyTheme"
tools:targetApi="31">
<activity
android:name=".RegisterActivity"
android:theme="@style/MyTheme"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/MyTheme">
<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/MyTheme">
</activity>
<activity android:name=".LoginActivity"
android:exported="true"
android:theme="@style/MyTheme">
</activity>
</application>
Keep in mind that in each activity.java i use getSupportActionBar();
My loginActivity that i highlighted in the error :
public class LoginActivity extends AppCompatActivity {
private EditText editTextLoginEmail, editTextLoginPwd;
private ProgressBar progressBar;
private FirebaseAuth authProfile;
private static final String TAG = "LoginActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.login_activity);
getSupportActionBar();
editTextLoginEmail = findViewById(R.id.editText_login_email);
editTextLoginPwd = findViewById(R.id.editText_login_password);
progressBar = findViewById(R.id.progressBar);
authProfile = FirebaseAuth.getInstance();
//Login Tab
Button buttonLogin = findViewById(R.id.button_login);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textEmail = editTextLoginEmail.getText().toString();
String textPwd = editTextLoginPwd.getText().toString();
if(TextUtils.isEmpty(textEmail)){
Toast.makeText(LoginActivity.this, "Please enter your email", Toast.LENGTH_SHORT).show();
editTextLoginEmail.setError("Email is required");
editTextLoginEmail.requestFocus();
} else if (!Patterns.EMAIL_ADDRESS.matcher(textEmail).matches()){
Toast.makeText(LoginActivity.this, "Please re-enter your email", Toast.LENGTH_SHORT).show();
editTextLoginEmail.setError("Valid Email is required");
editTextLoginEmail.requestFocus();
} else if (TextUtils.isEmpty(textPwd)){
Toast.makeText(LoginActivity.this, "Please enter your password", Toast.LENGTH_SHORT).show();
editTextLoginPwd.setError("Password is required");
editTextLoginPwd.requestFocus();
} else {
progressBar.setVisibility(View.VISIBLE);
loginUser (textEmail, textPwd);
}
}
});
It would be obvious in loginActivity.java that some lines were commented out and was causing the NPE.
All corrected and ActionBar is working fine.
Solution
This problem might be caused by your theme. Check it again, and make sure that it parents with the "Theme.MaterialComponents.DayNight.DarkActionBar".
<style name="MyTheme" parent=""Theme.MaterialComponents.DayNight.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
Answered By - S_i_l_e_n_t C_o_d_e_r
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.