Android Error [Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference]
Your code is throwing on com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95)
:
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
The problem is pretty simple- your Activity
is inheriting from the new android.support.v7.app.ActionBarActivity
. You should be using a call to getSupportActionBar()
instead of getActionBar()
.
If you look above around line 65 of your code you'll see that you're already doing that:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// TODO: Remove the redundant calls to getSupportActionBar()
// and use variable actionBar instead
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And then lower down around line 87 it looks like you figured out the same:
getSupportActionBar().setTitle(
Html.fromHtml("<font color=\"black\">" + mTitle + " - "
+ menutitles[0] + "</font>"));
// getActionBar().setTitle(mTitle +menutitles[0]);
Notice how you commented out getActionBar()
.
Android Error : Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
On your MainActivity
you are trying to hide the Action Bar
by using the code getActionBar().hide
,
But the problem is your MainActivity
doesn't have an Action Bar
because your Main Activity
is using the Theme
called Theme.Shopit
which removes the Action Bar
by default because its parent is Theme.MaterialComponents.DayNight.NoActionBar
.
So to fix this issue you can simply remove the code getActionBar().hide
from your MainActivity
.
Shwing Error : Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)
Try this
Add this in onCreate
ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Add this outside of the onCreate, this is used to navigate back to the parent activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
And also set Parent activity
<activity
android:name=".activity.CreateAccountActivity"
android:parentActivityName=".activity.LoginActivity" />
How Attempt to invoke virtual method on a null object reference
You are getting NullPointerException
because you are binding your recyclerView
in the wrong way
in fragments you need to use an inflated view
or getView()
for findViewById
Use this
recyclerView = view.findViewById(R.id.userList);
instead of this
recyclerView = getActivity().findViewById(R.id.userList);
Recycler View : Attempt to invoke virtual method on a null object reference
As I understand from fragment_home.xml
You have a RecyclerView
inside of HomeFragment
xml layout. But you are tryin to find the View
inside MainActivity
.
The issue in your MainActivity's onCreate
method.
When you call :
RecyclerView recyclerView = findViewById(R.id.mRecyclerView);
From MainActivity
It's trying to find a View
with id: R.id.mRecyclerView
in you activity_main.xml
(xml layout that you're using for MainActivity). But as I understand it's absent there, because it's inside fragment_home.xml
.
Your can read how find View::findViewById
works here:
https://developer.android.com/reference/android/view/View#findViewById(int)
Finds the first descendant view with the given ID, the view itself if
the ID matches getId(), or null if the ID is invalid (< 0) or there is
no matching view in the hierarchy.
So, When it can't find the view by id, it returns null.
Then you try to set Adapter
to RecyclerView
's variable which actually is null.
recyclerView.setAdapter(adapter); //recyclerView is null here
That's the reason of your issue.
You have 2 options.
- Move
RecyclerView
fromfragment_home.xml
toactivity_main.xml
, than it will be worked. - Another option more suitable in my opinion, since you've already used fragments:
Move your code related to RecyclerView
handling to HomeFragment
class and call findViewById
inside HomeFragment
.
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.exists()' on a null object reference
you get this exception because the method "getOutputMediaFile" return null and you try to use null object.
first, add null check to file, its always good approach.
second, you have to understand why this method return null and not the file you want.
if you try to use "getExternalStorageDirectory" and call mkdirs(), its depracated, you should use "getExternalFilesDir" on your context/activity.
try to do the change, if it does not help, please share with us the method "getOutputMediaFile" so we can see whats going on there and give you further help.
Related Topics
Cannot Catch Toolbar Home Button Click Event
How to Fix: "You Need to Use a Theme.Appcompat Theme (Or Descendant) with This Activity"
How to Send File Using Bluetooth on Android Programatically
Is Ondestroy Not Always Called
Wrong Requestcode in Onactivityresult
How to Generate Buildconfigfield with String Type
How to Exit When Back Button Is Pressed
Camera Tutorial for Android (Using Surfaceview)
Listselector Applies to the Entire List
Android: Unable to Instantiate Activity/Classnotfoundexception
Android:Fill Spinner from Java Code Programmatically
Android Resources$Notfoundexception: Resource Id #0X7F030027
Importing Module in Android Studio
Upload Video to Facebook in Android
Disable Logcat Output Completely in Release Android App
Cannot Install Signed APK to Device Manually, Got Error "App Not Installed"