无法使导航抽屉正常工作



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

这是我的代码:

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>

如果你在那里得到一个 NPE,那么mDrawerLayout必须为 null。确保drawer_layout是正确的 ID 才能在 XML 中查找抽屉。您还需要膨胀抽屉布局XML文件。你几乎是正确的(你需要用你的文件名替换下面的menu_welcome):

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

将下面的代码放在您的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);

使用前请仔细阅读示例代码

相关内容

  • 没有找到相关文章

最新更新