`Error in text java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flavourbasket.flavourbasket/com.flavourbasket.flavourbasket.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at com.flavourbasket.flavourbasket.MainActivity.navigationDrawer(MainActivity.java:50) at com.flavourbasket.flavourbasket.MainActivity.onCreate(MainActivity.java:30) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
`https://github.com/nippynic93/FlavourBaske
This link refers to my project which has the issue.
This is the Code Which has the error at 50th line. I am unable to find it please help me.
It contains Search Bar at the mainActivity. If I am going to comment the 50th line so my action bar got to disappear. And project runs fine.
package com.flavourbasket.flavourbasket;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mdrawerlayout;
private ActionBarDrawerToggle mtoggle;
NavigationView navigationview;
BottomNavigationView mBottomNav;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onInitialize();
navigationDrawer();
sideView();
videoView = findViewById(R.id.videoView);
videoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.video);
videoView.start();
}
public void onInitialize() {
mdrawerlayout = (DrawerLayout) findViewById(R.id.drawer);
navigationview = (NavigationView) findViewById(R.id.navigation);
mBottomNav = (BottomNavigationView) findViewById(R.id.bottom_navigation);
}
public void navigationDrawer() {
mtoggle = new ActionBarDrawerToggle(this, mdrawerlayout, R.string.open, R.string.close);
mdrawerlayout.addDrawerListener(mtoggle);
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.youtube_videos:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/user/flavourbasket"));
startActivity(i);
mdrawerlayout.closeDrawers();
break;
}
return true;
}
});
}
public void sideView() {
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// handle desired action here
// One possibility of action is to replace the contents above the nav bar
// return true if you want the item to be displayed as the selected item
switch (item.getItemId()) {
case R.id.extra_id:
Intent i = new Intent(MainActivity.this, Extras.class);
startActivity(i);
break;
}
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mtoggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}