0

我试图让用户通过按导航抽屉中的应用程序图标来打开我的导航抽屉。但是当我打开我尝试的活动时,我的 LogCat 在第 55 行给了我一个 NullPointerException。这是我的代码:

添加活动.java:

package de.hoffmann.pod2go;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class AddActivity extends Activity {

    public ActionBarDrawerToggle mDrawerToggle;

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

        setContentView(R.layout.activity_add);
        // Show the Up button in the action bar.

        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout, /* DrawerLayout object */
               R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.title_activity_add);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.drawer_title);
            }
        };


        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true); // Pressing the app icon in the action bar will navigate to the parent activity.
        getActionBar().setHomeButtonEnabled(true);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
         super.onPostCreate(savedInstanceState);
         // Sync the toggle state after onRestoreInstanceState has occurred.
         mDrawerToggle.syncState();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // Pass the event to ActionBarDrawerToggle, if it returns
         // true, then it has handled the app icon touch event
         if (mDrawerToggle.onOptionsItemSelected(item)) {
             return true;
         }


         switch (item.getItemId()) 
            {
                case android.R.id.home:
                    NavUtils.navigateUpFromSameTask(this);
                    return true;
            }

         return super.onOptionsItemSelected(item);
    }



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

activty_add.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">

    <!-- The main content view -->

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/add_activity_feed_heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/add_feed_url"
            android:layout_alignParentRight="true" />

        <EditText
            android:id="@+id/feed_heading"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="14dp"
            android:ems="10"
            android:hint="@string/url_hint"
            android:layout_alignParentLeft="true"  />            

        <!--  Button bar -->
        <LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="horizontal"
            android:layout_alignParentBottom="true" >
            <Button
                style="@style/buttonBarButton"
                android:text="@string/buttonbar_left"
                android:onClick="goBack"/>
            <Button
                style="@style/buttonBarButton"
                android:text="@string/buttonbar_right"/>
        </LinearLayout>

    </RelativeLayout>

    <!-- Navigation Drawer -->
    <ListView android:id="@+id/left_drawer"
        style="@style/NavigationDrawer"/>

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

另外,当它工作时,我是否必须将所有代码粘贴到每个活动文件中?

4

2 回答 2

1

我想问题出在第 25 行ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(

您刚刚使用全局变量创建了一个同名的局部变量mDrawerToggle 删除第ActionBarDrawerToggle25 行中的第一个可能会使您的代码工作

希望这有帮助。

于 2013-11-11T20:02:07.373 回答
1

您只需将引用放入本地对象

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(....

只需删除类的名称,以便将引用设置为全局对象值 :)

像这样说

mDrawerToggle = new ActionBarDrawerToggle(...

于 2015-02-21T12:54:46.597 回答