0

`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 error while crashing

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);
    }
}

1 Answers1

0

The ActionBar can be null. The code below will work:

    toolbar = findViewById(R.id.tools);
    setSupportActionBar(toolbar);

    ActionBar ab = ((AppCompatActivity)getActivity()).getSupportActionBar();

    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
    }
Anky An
  • 620
  • 7
  • 19
  • I have added that But getActivity() is coming as the error. Now what ? – Vivek Pathak Mar 28 '18 at 05:49
  • My mistake. I couldn't run your code and gave an answer without fully understanding your code. I used navigation drawer before and will check my code to get an answer. – Anky An Mar 28 '18 at 06:08
  • Okay. So please check that and please answer me. I need a reply as soon as possible. – Vivek Pathak Mar 28 '18 at 06:10
  • @VivekPathak you need answer as soon as possible then start learning android as soon as possible :P – Vidhi Dave Mar 28 '18 at 06:16
  • @VivekPathak I still can't run your code. After checking my code, the only difference between mine and yours is the code to close the drawer, use: mDrawerLayout.closeDrawer(mNavigationView); Also, you have to improve your debugging skills. Based on the error log, the error occurred within the OnNavigationItemSelectedListener. The problem may not be line 49. For example, you can log it to see if the action bar is null – Anky An Mar 28 '18 at 06:36
  • 1
    @VishvaDave Hahaha Agree with, Sir But I really have to submit my within 3 days in my college that's why I said, sir. Please don't mind it – Vivek Pathak Mar 28 '18 at 06:42