2014-05-09 70 views
-2

中拋出錯誤我嘗試創建一個簡單的listview活動,其中字符串數組從string.xml中讀取數組字符串,從而引發錯誤。我已經發布的代碼和下面登錄,從string.xml中讀取字符串數組在android

SimpleListViewActivity:

import android.annotation.TargetApi; 
import android.app.ListActivity; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.NavUtils; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class SimpleListViewActivity extends ListActivity { 
    String[] presidents = getResources().getStringArray(R.array.android_os_flavours); 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // ---no need to call this--- 
     // setContentView(R.layout.main); 
     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, presidents)); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) { 
     Toast.makeText(this, "You have selected " + presidents[position], 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

String.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Sethu Vignesh</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="action_settings">Settings</string> 
    <string name="title_activity_simple_list_view">Simple ListView</string> 
    <string name="title_activity_multi_choice">MultiChoice Listview</string> 
    <string name="title_activity_single_choice">Single Choice ListView</string> 
    <string name="title_activity_expandable_list_view">Expandable List View</string> 
    <string-array name="android_os_flavours"> 
     <item>Android 1.1 Banana Bread(API level 2)</item> 
     <item>Android 1.5 Cupcake (API level 3)</item> 
     <item>Android 1.6 Donut (API level 4)</item> 
     <item>Android 2.0 Eclair (API level 5)</item> 
     <item>Android 2.0.1 Eclair (API level 6)</item> 
     <item>Android 2.1 Eclair (API level 7)</item> 
     <item>Android 2.2–2.2.3 Froyo (API level 8)</item> 
     <item>Android 2.3–2.3.2 Gingerbread (API level 9)</item> 
     <item>Android 2.3.3–2.3.7 Gingerbread (API level 10)</item> 
     <item>Android 3.0 Honeycomb (API level 11)</item> 
     <item>Android 3.1 Honeycomb (API level 12)</item> 
     <item>Android 3.2 Honeycomb (API level 13)</item> 
     <item>Android 4.0–4.0.2 Ice Cream Sandwich (API level 14)</item> 
     <item>Android 4.0.3–4.0.4 Ice Cream Sandwich (API level 15)</item> 
     <item>Android 4.1 Jelly Bean (API level 16)</item> 
     <item>Android 4.2 Jelly Bean (API level 17)</item> 
     <item>Android 4.3 Jelly Bean (API level 18)</item> 
     <item>Android 4.4 KitKat (API level 19)</item> 
    </string-array> 


</resources> 

登錄:

05-09 17:40:09.332: E/AndroidRuntime(1320): FATAL EXCEPTION: main 
05-09 17:40:09.332: E/AndroidRuntime(1320): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sethuvignesh/com.example.sethuvignesh.SimpleListViewActivity}: java.lang.NullPointerException 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.os.Looper.loop(Looper.java:137) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at dalvik.system.NativeStart.main(Native Method) 
05-09 17:40:09.332: E/AndroidRuntime(1320): Caused by: java.lang.NullPointerException 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at com.example.sethuvignesh.SimpleListViewActivity.<init>(SimpleListViewActivity.java:16) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at java.lang.Class.newInstanceImpl(Native Method) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at java.lang.Class.newInstance(Class.java:1319) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) 
05-09 17:40:09.332: E/AndroidRuntime(1320):  ... 11 more 
05-09 17:40:09.352: D/dalvikvm(1320): GC_CONCURRENT freed 134K, 3% free 8222K/8391K, paused 13ms+0ms, total 18ms 

回答

3

移動這個

String[] presidents = getResources().getStringArray(R.array.android_os_flavours); 

onCreate內部

String[] presidents; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    presidents = getResources().getStringArray(R.array.android_os_flavours); 

getResources()需要Context。一旦創建活動,就可以使用Context

-1

我認爲這樣的:

String[] presidents = getResources().getStringArray(R.array.android_os_flavours); 

應該是:

String[] presidents = getResources().getStringArray(R.string.android_os_flavours); 

因爲它在strings.xml檔案,而不是在arrays.xml文件

+0

的http://開發商.android.com /引導/主題/資源/串resource.html。它的一個字符串數組''' – Raghunandan

相關問題