2014-03-05 60 views
0

當我嘗試創建SimpleAdapter的新實例時,出現錯誤NullPointerException。我不太確定這裏發生了什麼事。我使用所有必需的參數將setAdapter傳遞給了我的Custom ArrayAdapter。請幫忙。提前致謝。針對ArrayAdapter的Android NullPointerException

這裏是我的代碼[錯誤是在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); 

     } 
    } 

而且,這裏是我的logcat錯誤:

03-04 21:44:33.106: E/AndroidRuntime(14515): FATAL EXCEPTION: main 
03-04 21:44:33.106: E/AndroidRuntime(14515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.MainActivity}: java.lang.NullPointerException 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.ActivityThread.access$600(ActivityThread.java:150) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.os.Looper.loop(Looper.java:213) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.ActivityThread.main(ActivityThread.java:5225) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at java.lang.reflect.Method.invokeNative(Native Method) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at java.lang.reflect.Method.invoke(Method.java:525) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at dalvik.system.NativeStart.main(Native Method) 
03-04 21:44:33.106: E/AndroidRuntime(14515): Caused by: java.lang.NullPointerException 
03-04 21:44:33.106: E/AndroidRuntime(14515): at com.example.android.navigationdrawerexample.MainActivity.onCreate(MainActivity.java:69) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.Activity.performCreate(Activity.java:5133) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
03-04 21:44:33.106: E/AndroidRuntime(14515): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230) 
+1

哪一行是你的MainActivity.java中的第69個? –

+0

是'mDrawerList' null? – ipohfly

回答

0

我認爲這個問題是沒有價值你SimpleAdapter

傳遞
new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1)); 

mPlanetTitles仍未使用

0

您沒有將任何值傳遞給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是要顯示在列表視圖列表。

相關問題