抽屉导航上的NullPointerException

时间:2015-03-07 00:04:56

标签: java android nullpointerexception

虽然我不是编程新手,但我是Android新手,而且我一直在处理我正在处理的应用程序的问题。

该应用是使用Google Maps Api的地图。我得到了那个工作,并决定我想要一个精美的抽屉导航配合它。这并不是那么顺利。

我可以看到错误是第74行,但我无法弄清楚如何处理它。我找了很多同样问题的人,但他们的理由似乎与我的不同之处在于收到这个错误。

这里的第74和第75行,这些都是错误的原因:

mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener()

这里有一切:

    package com.example.lehi.byu_idahomaps;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.PopupMenu;
import android.view.MenuInflater;


import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends ActionBarActivity implements PopupMenu.OnMenuItemClickListener {

    // Locations for all buildings on campus.
    private final LatLng LOCATION_CAMPUS = new LatLng(43.815489, -111.783012);
    private final LatLng LOCATION_SMITH = new LatLng(43.819230, -111.781481);
    private final LatLng LOCATION_HART = new LatLng(43.819780, -111.785261);
    private final LatLng LOCATION_RICKS = new LatLng(43.815120, -111.781303);
    private final LatLng LOCATION_AUSTIN = new LatLng(43.816018, -111.784543);
    private final LatLng LOCATION_ICENTER = new LatLng(43.818782, -111.785176);
    private final LatLng LOCATION_BENSON = new LatLng(43.815569, -111.783255);
    private final LatLng LOCATION_CLARKE = new LatLng(43.820492, -111.781796);
    private final LatLng LOCATION_HEALTH = new LatLng(43.817017, -111.779275);
    private final LatLng LOCATION_HINCKLEY = new LatLng(43.816080, -111.779876);
    private final LatLng LOCATION_KIMBALL = new LatLng(43.817249, -111.781506);
    private final LatLng LOCATION_KIRKHAM = new LatLng(43.821336, -111.781657);
    private final LatLng LOCATION_MC = new LatLng(43.818619, -111.782611);
    private final LatLng LOCATION_MCKAY = new LatLng(43.819587, -111.783159);
    private final LatLng LOCATION_ROMNEY = new LatLng(43.820423, -111.783148);
    private final LatLng LOCATION_SNOW = new LatLng(43.821414, -111.783652);
    private final LatLng LOCATION_SPORI = new LatLng(43.821019, -111.782300);
    private final LatLng LOCATION_TAYLOR = new LatLng(43.817171, -111.782408);

    // Our map
    private GoogleMap map; // Might be null if Google Play services APK is not available.
    // Our marker object
    Marker marker;

    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    String[] mDrawerListItems;

    //Creates the map, sets it to the campus location, and turns on your location
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        CameraUpdate start = CameraUpdateFactory.newLatLngZoom(LOCATION_CAMPUS, 15);
        map.moveCamera(start);
        map.setMyLocationEnabled(true);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mDrawerList = (ListView)findViewById(android.R.id.list);
        mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int editedPosition = position + 1;
                Toast.makeText(MapsActivity.this, "You selected item " + editedPosition, Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawer(mDrawerList);
            }
        });
        mDrawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout,
                toolbar,
                R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View v) {
                super.onDrawerClosed(v);
                invalidateOptionsMenu();
                syncState();
            }

            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
                invalidateOptionsMenu();
                syncState();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mDrawerToggle.syncState();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                    mDrawerLayout.closeDrawer(mDrawerList);
                } else {
                    mDrawerLayout.openDrawer(mDrawerList);
                }
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public void showPopUp(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        popup.setOnMenuItemClickListener(MapsActivity.this);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.popup, popup.getMenu());
        popup.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        CameraUpdate update;
        switch (item.getItemId()) {
            case R.id.austin:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_AUSTIN, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_AUSTIN)
                        .title("Mark Austin")
                        .snippet("College of Physical Sciences & Engineering"));
                map.animateCamera(update);
                return true;

            case R.id.icenter:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_ICENTER, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_ICENTER)
                        .title("BYU-I Center"));
                map.animateCamera(update);
                return true;

            case R.id.benson:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_BENSON, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_BENSON)
                        .title("Ezra Taft Benson")
                        .snippet("College of Agriculture and Life Sciences"));
                map.animateCamera(update);
                return true;

            case R.id.clarke:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_CLARKE, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_CLARKE)
                        .title("John L. Clarke"));
                map.animateCamera(update);
                return true;

            case R.id.hart:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_HART, 19);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_HART)
                        .title("John W. Hart"));
                map.animateCamera(update);
                return true;

            case R.id.health:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_HEALTH, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_HEALTH)
                        .title("Student Health Center"));
                map.animateCamera(update);
                return true;

            case R.id.hinckley:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_HINCKLEY, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_HINCKLEY)
                        .title("Gordan B. Hinckley")
                        .snippet("College of Education & Human Development"));
                map.animateCamera(update);
                return true;

            case R.id.kimball:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_KIMBALL, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_KIMBALL)
                        .title("Spencer W. Kimball"));
                map.animateCamera(update);
                return true;

            case R.id.kirkham:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_KIRKHAM, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_KIRKHAM)
                        .title("Oscar A. Kirkham"));
                map.animateCamera(update);
                return true;

            case R.id.mc:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_MC, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_MC)
                        .title("Hyrum Manwaring"));
                map.animateCamera(update);
                return true;

            case R.id.mckay:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_MCKAY, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_MCKAY)
                        .title("David O. McKay Library"));
                map.animateCamera(update);
                return true;

            case R.id.ricks:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_RICKS, 19);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_RICKS)
                        .title("Thomas E. Ricks"));
                map.animateCamera(update);
                return true;

            case R.id.romney:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_ROMNEY, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_ROMNEY)
                        .title("George S. Romney"));
                map.animateCamera(update);
                return true;

            case R.id.smith:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_SMITH, 19);
                map.animateCamera(update);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_SMITH)
                        .title("Joseph Fielding Smith")
                        .snippet("College of Business and Communication\n" +
                                "College of Language & Letters"));
                return true;

            case R.id.snow:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_SNOW, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_SNOW)
                        .title("Eliza R. Snow"));
                map.animateCamera(update);
                return true;

            case R.id.spori:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_SPORI, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_SPORI)
                        .title("Jacob Spori")
                        .snippet("College of Performing & Visual Arts"));
                map.animateCamera(update);
                return true;

            case R.id.taylor:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_TAYLOR, 18);
                marker = map.addMarker(new MarkerOptions()
                        .position(LOCATION_TAYLOR)
                        .title("Taylor"));
                map.animateCamera(update);
                return true;

            default:
                if (marker != null) {
                    marker.remove();
                }
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                update = CameraUpdateFactory.newLatLngZoom(LOCATION_CAMPUS, 15);
                map.animateCamera(update);
                return true;
        }

    }

}

这是我的activity_maps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity"
    android:orientation="vertical">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        xmlns:android="http://schemas.android.com/apk/res/android" />


    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context=".MapsActivity"
                    android:clickable="false"
                    android:background="#2a6ebb"
                    android:orientation="vertical">

                <ImageButton
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:id="@+id/btnmenu"
                    android:layout_width="71dp"
                    android:layout_height="65dp"
                    android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
                    android:onClick="showPopUp"
                    android:background="#2a6ebb"
                    android:contentDescription="Slide out menu button" />

                <TextView
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="wrap_content"
                    android:layout_height="53dp"
                    android:id="@+id/edittext"
                    android:gravity="center_vertical|center_horizontal"
                    android:text="BYU-I Maps"
                    android:textSize="35sp"
                    android:textIsSelectable="false"
                    android:layout_gravity="center_horizontal"
                    android:layout_above="@+id/map"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentTop="true" />

                <fragment
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/btnmenu" />
            </RelativeLayout>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ListView
            android:id="@+id/list"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />


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

</LinearLayout>

这是血腥的错误:

03-06 16:46:51.836  15778-15778/com.example.lehi.byu_idahomaps E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.lehi.byu_idahomaps, PID: 15778
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lehi.byu_idahomaps/com.example.lehi.byu_idahomaps.MapsActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497)
            at android.app.ActivityThread.access$900(ActivityThread.java:168)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5678)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.lehi.byu_idahomaps.MapsActivity.onCreate(MapsActivity.java:74)
            at android.app.Activity.performCreate(Activity.java:5586)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497)
            at android.app.ActivityThread.access$900(ActivityThread.java:168)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5678)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

mDrawerList = (ListView)findViewById(android.R.id.list);

应改为:

mDrawerList = (ListView)findViewById(R.id.list);

还删除ID为content_frame的FrameLayout DrawerLayout应包含一个内容View(Group)和一个列表或片段(将成为抽屉)

答案 1 :(得分:0)

删除{}中的android.

mDrawerList = (ListView)findViewById(android.R.id.list);

这将导致:

mDrawerList = (ListView)findViewById(R.id.list);