findViewById(R.id.drawer_layout)返回null

时间:2014-02-15 21:17:01

标签: java android android-layout

我遇到以下问题:

mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

返回null。

出于调试目的,我尝试了官方Android DrawerLayout教程中的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 -->
<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<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>

这是我的FragmentActivity文件的顶部:

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class PuzzleAllActivity extends FragmentActivity {

private ProgressDialog dialog;

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_puzzle_all);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    if(mDrawerLayout == null){
        Log.d("DEBUG", "Drawer layout is null");
    }
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.menu_icon, R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            //getActionBar().setTitle(mTitle);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
                invalidateOptionsMenu();
            }
        }

        public void onDrawerOpened(View drawerView) {
            //getActionBar().setTitle(mDrawerTitle);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
                invalidateOptionsMenu();
            }
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

由于某种原因,mDrawerLayout为null,这给了我最后一行的java.lang.NullPointerException:

mDrawerLayout.setDrawerListener(mDrawerToggle); 

我对Android编程相对较新,并且不知道可能导致此问题的原因。

我确保检查项目构建路径中的支持库。

这是我得到的错误:

02-15 22:12:03.712: E/AndroidRuntime(17259): FATAL EXCEPTION: main
02-15 22:12:03.712: E/AndroidRuntime(17259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.puzzleall.puzzleall/com.puzzleall.puzzleall.PuzzleAllActivity}: java.lang.NullPointerException
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.os.Looper.loop(Looper.java:176)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.main(ActivityThread.java:5279)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at java.lang.reflect.Method.invoke(Method.java:511)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at dalvik.system.NativeStart.main(Native Method)
02-15 22:12:03.712: E/AndroidRuntime(17259): Caused by: java.lang.NullPointerException
02-15 22:12:03.712: E/AndroidRuntime(17259):    at com.puzzleall.puzzleall.PuzzleAllActivity.onCreate(PuzzleAllActivity.java:71)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.Activity.performCreate(Activity.java:5267)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)

我感谢我能得到的任何帮助:)

1 个答案:

答案 0 :(得分:0)

NullPointer异常是由不同的布局文件(“layout”和“layout-land”)引起的。

Landscape版本有一个没有DrawerLayout的旧布局文件。所以当它被夸大时,我在引用DrawerLayout时得到了一个N​​ullPointer异常。