I have created a project int activity_main and want to add a Navigation drawer to it. I have gone into the java folder. Right click > new > Actvity > Navigation Drawer activity.

Note: I DON't have an action bar, to create the menu button(3 stacked lines)

Now I don't know how to actually open the navigation drawer over activity_main, without the app crashing. Thanks.

I've tried doing an on click listener with a button which has the drawer layout defined as dl (DrawerLayout dl = (DrawerLayout)findviewbyIOd(R.id.Drawer_layout))

and have done dl.openDrawer(Gravity.LEFT); have tried putting the code from the NavDrawer activity into the activity_main, but app won't start.

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
Switch OnOff;

EditText editText;
int NumberPicked;
int NumberPicked2;
private DrawerLayout dl;
private ActionBarDrawerToggle abdt;
Values[] troughvals = new Values[8];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BobbleSelector BS = new BobbleSelector();
        final DrawerLayout dl = (DrawerLayout)findViewById(R.id.dl);
        Button btnChange = (Button) findViewById(R.id.btnChange);
        Switch OnOff = (Switch) findViewById(R.id.OnOff);
        NumberPicker numberPicker2 = findViewById(R.id.numberPicker2);
        NumberPicker numberPicker = findViewById(R.id.numberPicker);
        numberPicker.setMinValue(00);
        numberPicker2.setMinValue(00);
        numberPicker2.setMaxValue(23);
        numberPicker.setMaxValue(23);
        btnChange.setOnClickListener( new View.OnClickListener(){
            public void onClick(View v){

                dl.openDrawer(Gravity.LEFT);

            }

        });
       OnOff.setTextOff("On");
       OnOff.setTextOn("Off");

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return abdt.onOptionsItemSelected(item) ||  super.onOptionsItemSelected(item);
    }

    public void handleSwitchClick(View view) {
        Switch s = (Switch) view;
        boolean isChecked = s.isChecked();
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked){
        switch (compoundButton.getId()){
            case R.id.OnOff:
                break;
        }
    }
}

Note: have disabled fab, because it could not be found in my project. I have created a custom Menu for the Navigation Drawer

public class BobbleSelector extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bobble_selector);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//        Button fab = findViewById(R.id.btnChange);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

        DrawerLayout drawer = findViewById(R.id.dl);

        NavigationView navigationView = findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.dl);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bobble_selector, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();



        DrawerLayout drawer = findViewById(R.id.dl);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Drawer Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_bobble_selector"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_bobble_selector"
        app:menu="@menu/nav_menu" />

</android.support.v4.widget.DrawerLayout>

From Logcat

2019-06-02 13:13:08.195 12745-12745/com.example.mytest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mytest, PID: 12745
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference
        at com.example.mytest.MainActivity$1.onClick(MainActivity.java:49)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        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:440)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Expected to open drawer with the changebutton.openDrawer(Gravity.Left) But crashes the app.

1 Answers

0
Skwangles On

Found I needed to move the code relating to the nav bar to the bottom of OnCreate and set the "SetContentView()" to be the layout for the Navigation bar.

Then it worked perfectly. Before

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btnChange);

        drawer = findViewById(R.id.dl);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer, R.string.navigation_drawer_open,R.string.navigation_drawer_close);

        drawer.addDrawerListener(toggle);
        toggle.syncState();

        //Not nav
        Switch OnOff = (Switch) findViewById(R.id.OnOff);
        NumberPicker numberPicker2 = findViewById(R.id.numberPicker2);
        NumberPicker numberPicker = findViewById(R.id.numberPicker);
        numberPicker.setMinValue(00);
        numberPicker2.setMinValue(00);
        numberPicker2.setMaxValue(23);
        numberPicker.setMaxValue(23);

       OnOff.setTextOff("On");
       OnOff.setTextOn("Off");
        //not nav

    }

After

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btnChange);


        //Not nav
        Switch OnOff = (Switch) findViewById(R.id.OnOff);
        NumberPicker numberPicker2 = findViewById(R.id.numberPicker2);
        NumberPicker numberPicker = findViewById(R.id.numberPicker);
        numberPicker.setMinValue(00);
        numberPicker2.setMinValue(00);
        numberPicker2.setMaxValue(23);
        numberPicker.setMaxValue(23);

       OnOff.setTextOff("On");
       OnOff.setTextOn("Off");
        //not nav
        setContentView(R.layout.activity_bobble_selector);
        drawer = findViewById(R.id.dl);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        assert drawer != null;
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }