简体   繁体   English

如何使用导航抽屉打开新活动?

[英]How to open new activities using navigation drawer?

I want to create a navigation drawer menu opened by sliding from left to right and I did it by a google developer tutorial, but the tutorial showed how to create the navigation drawer and how to set the item from the menu to open new fragment layouts, but it's not what I wanted.. what I want is to open new activities with the item from the navigation drawer menu, and I did it too, but when the Sliding.java activity has to open, my app crashes.. Sliding.java is the activity which contains the codes for the menu . 我想创建一个从左向右滑动打开的导航抽屉菜单,并由Google开发人员教程完成,但是该教程显示了如何创建导航抽屉以及如何从菜单中设置项目以打开新的片段布局,但这不是我想要的。我想要的是使用导航抽屉菜单中的项目打开新活动,我也这样做了,但是当必须打开Sliding.java活动时,我的应用程序崩溃了。是包含菜单代码的活动。

Sliding.java: Sliding.java:

package com.orar.cngcnasaud;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class Sliding extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

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

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    private void selectItem(int i) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return false;


        }



      private  class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }

    }




    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }



    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        mDrawerToggle.onConfigurationChanged(newConfig);
    }

        @SuppressWarnings("unused")
        private void selectItem1(final int position) {
            switch(position) {
            case 1:
                    Intent a = new Intent(this, MainActivity.class);
                    startActivity(a);
            break;
            case 2:
                    Intent b = new Intent(this, Profesori.class);
                   startActivity(b);
                   break;
            default:
            }
        }
}

I have no errors when I run it, but it's still crashing.. 运行它时没有任何错误,但仍然崩溃。

EDIT: 编辑:

LOGCAT: logcat的:

02-17 16:29:58.496: I/dalvikvm(322): Could not find method com.orar.cngcnasaud.Sliding.getActionBar, referenced from method com.orar.cngcnasaud.Sliding.onCreate
02-17 16:29:58.496: W/dalvikvm(322): VFY: unable to resolve virtual method 5311: Lcom/orar/cngcnasaud/Sliding;.getActionBar ()Landroid/app/ActionBar;
02-17 16:29:58.496: D/dalvikvm(322): VFY: replacing opcode 0x6e at 0x0058
02-17 16:29:58.496: D/dalvikvm(322): VFY: dead code 0x005b-0087 in Lcom/orar/cngcnasaud/Sliding;.onCreate (Landroid/os/Bundle;)V
02-17 16:29:58.504: I/dalvikvm(322): Could not find method com.orar.cngcnasaud.Sliding.getActionBar, referenced from method com.orar.cngcnasaud.Sliding.setTitle
02-17 16:29:58.504: W/dalvikvm(322): VFY: unable to resolve virtual method 5311: Lcom/orar/cngcnasaud/Sliding;.getActionBar ()Landroid/app/ActionBar;
02-17 16:29:58.504: D/dalvikvm(322): VFY: replacing opcode 0x6e at 0x0002
02-17 16:29:58.504: D/dalvikvm(322): VFY: dead code 0x0005-000b in Lcom/orar/cngcnasaud/Sliding;.setTitle (Ljava/lang/CharSequence;)V
02-17 16:29:58.534: D/AndroidRuntime(322): Shutting down VM
02-17 16:29:58.534: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-17 16:29:58.554: E/AndroidRuntime(322): FATAL EXCEPTION: main
02-17 16:29:58.554: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orar.cngcnasaud/com.orar.cngcnasaud.Sliding}: java.lang.NullPointerException
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.os.Looper.loop(Looper.java:123)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-17 16:29:58.554: E/AndroidRuntime(322):  at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:29:58.554: E/AndroidRuntime(322):  at java.lang.reflect.Method.invoke(Method.java:521)
02-17 16:29:58.554: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 16:29:58.554: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 16:29:58.554: E/AndroidRuntime(322):  at dalvik.system.NativeStart.main(Native Method)
02-17 16:29:58.554: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
02-17 16:29:58.554: E/AndroidRuntime(322):  at com.orar.cngcnasaud.Sliding.onCreate(Sliding.java:39)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 16:29:58.554: E/AndroidRuntime(322):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-17 16:29:58.554: E/AndroidRuntime(322):  ... 11 more
02-17 16:30:06.275: I/Process(322): Sending signal. PID: 322 SIG: 9

sliding.xml: sliding.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bff">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

You have a NullPointer here: 您在此处有一个NullPointer:

Caused by: java.lang.NullPointerException at com.orar.cngcnasaud.Sliding.onCreate(Sliding.java:39)

This seems to be your DrawerLayout (not sure about the line anyway): 这似乎是您的DrawerLayout(无论如何都不确定该行):

mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

Can you check/post your layout? 您可以检查/张贴布局吗?

I had the same problem. 我有同样的问题。 So I solved it using the following steps. 因此,我使用以下步骤解决了该问题。 It ll be useful to others. 对其他人有用。

The problem is because the compiler searching drawer_layout in the activity_main.xml . 问题是,因为编译器搜索drawer_layoutactivity_main.xml

setContentView(R.layout.activity_main); 
//You added the view file as activity_main 

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
//While execution the compiler will search "drawer_layout" in the view file (activity_main.xml). But the drawer_layout is in sliding.xml.

But there is no "drawer_layout" is added in the activity_main.xml . 但是在activity_main.xml没有添加"drawer_layout" Its added in the "sliding.xml" 它添加在"sliding.xml"

So, To solve this problem we need to add the following in the activity_main.xml. 因此,要解决此问题,我们需要在activity_main.xml中添加以下内容。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bff">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

<!-- The content in the activity_main.xml -->

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

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

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