导航抽屉无法正常工作

时间:2015-01-24 05:15:41

标签: android android-studio navigation-drawer drawertoggle

我已经在这里工作了好几个小时,似乎根本无法让抽屉出现。这是我的代码也许有人可以告诉我这里有什么问题...应用程序运行没有错误,直到我使用抽屉活动然后它崩溃。

这是我的代码:

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v7.app.ActionBarDrawerToggle;

public class FlavorListView extends ActionBarActivity  {

    //shared preference
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;


    @Override
    public boolean onCreateOptionsMenu(Menu action_setting) {
        //MenuInflater inflater = getMenuInflater();
        //inflater.inflate(R.menu.menu_welcome,action_setting);

        //drawer
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
        mDrawerToggle = new ActionBarDrawerToggle(FlavorListView.this, mDrawerLayout,
                R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
       mDrawerLayout.setDrawerListener(mDrawerToggle);
        return true;
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        //return super.onPrepareOptionsMenu(menu);

        //drawer end

        return true;



    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_flavor);



        String[] select_flavor_list = {"Kansas City", "Memphis", "Texas", "North Carolina", "South Carolina","Alabama"};


        ListAdapter FlavorListAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_flavor, R.id.flavor_text_view,
                select_flavor_list);

        ListView listView = (ListView) findViewById(R.id.listView);

        listView.setAdapter(FlavorListAdapter);


        //For list view selection, begin here...
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //String flavorPicked = String.valueOf(parent.getItemAtPosition(position));

                FoodStyleDialog cdd = new FoodStyleDialog(FlavorListView.this);
                cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                cdd.show();
                return true;

            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
                //GlobalClass flavorPref = ((GlobalClass)getApplicationContext());

                String flavorPicked = String.valueOf(adapterView.getItemAtPosition(position));

                if (flavorPicked.equals("Kansas City")) {
                    Intent intent = new Intent(FlavorListView.this, KansasCity.class);
                    startActivity(intent);
                    setContentView(R.layout.kansas_city);
                    Toast.makeText(FlavorListView.this, "Looks like " + flavorPicked +
                                    " style be your flavor! You can change your flavor anytime in the settings menu.",
                            Toast.LENGTH_LONG).show();


                }
                else if (flavorPicked.equals("Memphis")) {
                    Intent intent = new Intent(FlavorListView.this, KansasCity.class);
                    startActivity(intent);
                    setContentView(R.layout.kansas_city);
                    Toast.makeText(FlavorListView.this, "Looks like " + flavorPicked +
                                    " style be your flavor! You can change your flavor anytime in the settings menu.",
                            Toast.LENGTH_LONG).show();
                }

            }

        });

    }
}

我收到java.lang.NullPointerException并且调试人员在(FlavorListView.java:57)mDrawerLayout.setDrawerListener(mDrawerToggle);

如果我评论这一行该应用程序运行但没有抽屉...

请帮忙。谢谢:))

抽屉布局xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navigation_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 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" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->

    <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="#ffff"/>
</android.support.v4.widget.DrawerLayout>

FlavorListView Acivity xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    tools:context=".FlavorListView"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="Choose Your Flavor"
        android:textAlignment="center"
        android:textSize="30sp"
        android:textStyle="bold"
        android:gravity="center_horizontal"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:listSelector="@drawable/abc_list_pressed_holo_light" />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

如果您在那里获得NPE,那么mDrawerLayout必须为空。确保drawer_layout是在XML中查找抽屉的正确ID。您还需要为Drawer Layout XML文件充气。你几乎把它弄好了(你需要用文件名替换下面的menu_welcome):

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_welcome, action_setting);
mDrawerLayout = (DrawerLayout) action_setting.findViewById(R.id.navigation_drawer);

答案 1 :(得分:0)

将以下代码放在onCreate而不是

onCreateOptionMenu

/ ------------------------------------------- ----------------- /

mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
        mDrawerToggle = new ActionBarDrawerToggle(FlavorListView.this, mDrawerLayout,R.drawable.ic_menu,
                R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
       mDrawerLayout.setDrawerListener(mDrawerToggle);

在使用之前仔细阅读示例代码