I am trying to set default item on activity created but it isn't working? This is my code:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_userhome); mTextMessage = (TextView) findViewById(R.id.message); profile = (FrameLayout) findViewById(R.id.profile); mall = (FrameLayout) findViewById(R.id.mall); dietplan =(FrameLayout) findViewById(R.id.dietplan); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setSelectedItemId(R.id.dietplan); navigation.setOnNavigationItemSelectedListener (mOnNavigationItemSelectedListener); }
But it seems that navigation.setSelectedItemId(R.id.dietplan);
is not working. Please help me to set default item of bottom navigation bar:
This is my stack trace(logcat):
FATAL EXCEPTION: main Process: gym.android.ommsoftware.gym, PID: 1915 java.lang.RuntimeException: Unable to start activity ComponentInfo{gym.android.ommsoftware.gym/gym.android.ommsoftware.gym.Userhome}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at gym.android.ommsoftware.gym.Userhome.onCreate(Userhome.java:57) at android.app.Activity.performCreate(Activity.java:5541) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method)
Instead of selected you need to setChecked(true)
that item. Try this code
mBottomNavigationView=(BottomNavigationView)findViewById(R.id.bottom_nav); mBottomNavigationView.getMenu().findItem(R.id.item_id).setChecked(true);
Checked item is highlighted in BottomNavigationView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With