Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArrayAdapter的Android NullPointerException_Android_Nullpointerexception_Android Arrayadapter - Fatal编程技术网

ArrayAdapter的Android NullPointerException

ArrayAdapter的Android NullPointerException,android,nullpointerexception,android-arrayadapter,Android,Nullpointerexception,Android Arrayadapter,当我尝试创建SimpleAdapter的新实例时,收到一个错误NullPointerException。我不太确定这里发生了什么事。我正在将setAdapter和所有必需参数传递给我的自定义ArrayAdapter。请帮忙。提前谢谢 这是我的代码[错误在第69行]: package com.example.android.navigationdrawerexample; import java.util.Locale; import android.app.Activity; import a

当我尝试创建SimpleAdapter的新实例时,收到一个错误NullPointerException。我不太确定这里发生了什么事。我正在将setAdapter和所有必需参数传递给我的自定义ArrayAdapter。请帮忙。提前谢谢

这是我的代码[错误在第69行]:

package com.example.android.navigationdrawerexample;

import java.util.Locale;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;  

public class MainActivity extends Activity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);   //PULLS RESOURCES FROM XML FILE
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);       

        // set up the drawer's list view with items and click listener

        //mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mPlanetTitles));

        mDrawerList.setAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon

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

            public void onDrawerClosed(View view) {

                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

            }

            public void onDrawerOpened(View drawerView) {

                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);   

        if (savedInstanceState == null) {

            selectItem(0);

        }
    }

我认为问题在于SimpleAdapter中没有传递值


mplanettiles保持未使用状态

您没有向SimpleAdapter传递任何值

而不是:

mDrawerList.setAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
试试这个

mDrawerList.setAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, mPlanetTitles));

其中,mPlanetTitles是要在列表视图中显示的列表。

哪一行是MainActivity.java中的第69行?mDrawerList是否为空?
mDrawerList.setAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
mDrawerList.setAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, mPlanetTitles));