简体   繁体   English

将导航抽屉从第二个活动添加到activity_main。 在主活动中使用.opendrawer打开第二个

[英]Adding navigation drawer from second activity to activity_main. Using .opendrawer in main activity to open second

I have created a project int activity_main and want to add a Navigation drawer to it. 我创建了一个项目int activity_main,并希望为其添加一个Navigation抽屉。 I have gone into the java folder. 我已进入java文件夹。 Right click > new > Actvity > Navigation Drawer activity. 右键单击>新建> Actvity>导航抽屉活动。

Note: I DON't have an action bar, to create the menu button(3 stacked lines) 注意:我没有操作栏,创建菜单按钮(3个堆叠线)

Now I don't know how to actually open the navigation drawer over activity_main, without the app crashing. 现在我不知道如何在没有应用程序崩溃的情况下在activity_main上实际打开导航抽屉。 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)) 我已经尝试使用一个按钮将点击监听器做了一个按钮,该按钮的抽屉布局定义为dl(DrawerLayout dl =(DrawerLayout)findviewbyIOd(R.id.Drawer_layout))

and have done dl.openDrawer(Gravity.LEFT); 并完成了dl.openDrawer(Gravity.LEFT); have tried putting the code from the NavDrawer activity into the activity_main, but app won't start. 尝试将NavDrawer活动中的代码放入activity_main,但app不会启动。

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. 注意:已禁用fab,因为在我的项目中找不到它。 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 来自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. 预计用changebutton.openDrawer(Gravity.Left)打开抽屉但是崩溃应用程序。

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. 发现我需要将与导航栏相关的代码移动到OnCreate的底部,并将“SetContentView()”设置为导航栏的布局。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM