0

I made a navigation drawer in my project, then I wanted to make a button for sliding left and right but my app stops and blocks.

I get this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{radiofm.arabelradio/radiofm.arabelradio.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

Program:

package radiofm.arabelradio;

import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import java.io.IOException;

import static android.media.AudioManager.STREAM_MUSIC;

public class MainActivity extends AppCompatActivity {


    ImageButton id_play;
    MediaPlayer mediaPlayer;
    boolean prepared = false;
    boolean started =false;
    String stream ="http://arabelfm.ice.infomaniak.ch/arabelprodcastfm.mp3";
    //main_page contain arabel_photo player for radio



    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /////////////////////////////////////////////////////////////////////////////
        id_play = (ImageButton) findViewById(R.id.id_play);
        id_play.setEnabled(false);
        //id_play.setText("LOADING");
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        new PlayerTask().execute(stream);
        id_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (started){
                    started=false;
                    mediaPlayer.pause();
                    id_play.setImageResource(R.drawable.play);
                   // id_play.setText("PLAY");
                }
                else {
                    started=true;
                    mediaPlayer.start();
                    id_play.setImageResource(R.drawable.pause);

                    //id_play.setText("PAUSE");
                }
            }
        });
        //main_page contain arabel_photo player for radio//////////////////////////:




        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);



    }




//////////////////////////////////////////////////////////////////////////////////////////////////////////
    class PlayerTask extends AsyncTask<String,Void,Boolean>{

        @Override
        protected Boolean doInBackground(String... strings) {

            try {
                mediaPlayer.setDataSource(strings[0] );
                mediaPlayer.prepare();
                prepared=true;
            } catch (IOException e) {
                e.printStackTrace();
            }

            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            id_play.setEnabled(true);
            id_play.setImageResource(R.drawable.play);

            // id_play.setText("PLAY");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(started){
            mediaPlayer.pause();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(started){
            mediaPlayer.start();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(prepared){
            mediaPlayer.release();
        }
    }
/////////////////////////////////////////////////////////////////////////////////////////////////////
    //main_page contain arabel_photo player for radio//////////////////////////:



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vladyslav Matviienko Jul 05 '17 at 12:44
  • Possible duplicate of [NullPointerException: with ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference](https://stackoverflow.com/questions/30681918/nullpointerexception-with-actionbar-setdisplayhomeasupenabledboolean-on-a-nu) – Dhanumjay Jul 05 '17 at 12:45
  • how can i fix it guys ? – mohammad alsaleh Jul 05 '17 at 12:48
  • replace getActionBar() with getSupportActionBar() – Dhanumjay Jul 05 '17 at 12:49
  • same errors i replaced it – mohammad alsaleh Jul 05 '17 at 12:53
  • post your theme – Dhanumjay Jul 05 '17 at 13:00
  • Did you initialize your toolbar view? You need to use something like Toolbar toolbar = (Toolbar) findViewById(R.id.yourToolbarViewId); or Buttterknife @BindView(R.id.yourToolbarViewId) Toolbar toolbar; and then setSupportActionBar(toolbar); in your onCreate() before calling any methods on toolbar object. – lidkxx Jul 05 '17 at 13:06
  • @lidkxx no i didnot , is it work ? – mohammad alsaleh Jul 05 '17 at 13:54
  • Try suggestions from my previous comment, also you might need to use getSupportActionBar() instead of getActionBar(). Of course when you declare object as class field, Android studio will add appropriate import, like so: import android.support.v7.widget.Toolbar; – lidkxx Jul 05 '17 at 13:57

2 Answers2

0

Your activity theme probably without action bar, and the function getActionBar() return null instance.

Alex
  • 9,102
  • 3
  • 31
  • 35
0

just add this in your code

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);