0

ナビゲーション ドロワーのアプリ アイコンを押して、ユーザーがナビゲーション ドロワーを開こうとしています。しかし、試してみるアクティビティを開くと、55 行目で LogCat から NullPointerException が返されます。これが私のコードです。

AddActivity.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;
    }
}

活動_追加.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(

グローバル変数と同じ名前のローカル変数を作成したところ、25行mDrawerToggle 目の最初の変数を削除すると、コードが機能する可能性がありますActionBarDrawerToggle

この助けを願っています。

于 2013-11-11T20:02:07.373 に答える