Issue
I don't know why BottomNavigationView
null. I'm using the Firebase API and I added some code to add data to JSON file. Now after I did that, one of my Activities just stopped working. It wasn't even the Activity I was working on. My navigation equals null and I checked the XML files. The names are the same and debugging doesn't help either, because it shows me that one line of code that sets the BottomNavigationView
and gives me a NullPointer.
This is the Navigation that is creating this problem:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_to_dos:
return true;
case R.id.navigation_categories:
intent = new Intent(ToDos.this, Categories.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
case R.id.navigation_account:
intent = new Intent(ToDos.this, Account.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
}
return false;
}
};
And this is the part where I initialize everything concerning the Navigaion in the onCreate() function:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_to_dos);
I get this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: lb335.bbbaden.ch.pendenzenverwaltung, PID: 25288
java.lang.RuntimeException: Unable to start activity ComponentInfo{lb335.bbbaden.ch.pendenzenverwaltung/lb335.bbbaden.ch.pendenzenverwaltung.ToDos}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
at lb335.bbbaden.ch.pendenzenverwaltung.ToDos.onCreate(ToDos.java:76)
at android.app.Activity.performCreate(Activity.java:7365)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
My Activity XML:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
I'm lost on this one and would really appreciate some help from more experienced Coders.
EDIT:
Thanks to snachmsm I found out, that I had my order of functions was all messed up. I executed findViewById()
before I actually set the content. I had to switch that out and now it works.
Solution
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
your BottomNavigationView
is null
, thats a fact. this line isn't fetching your View
:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
are you shure your BottomNavigationView
belongs to inflated layout? R.id.navigation
is set properly for BottomNavigationView
? show some XML and more code from onCreate
method. maybe inproper order? (e.g. findViewById
called before setContentView
)
edit: ok, id
is set properly for View
inside XML, but you posted only small part of it, show also how do you inflate this XML inside Activity
Answered By - snachmsm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.