我尝试创建一个简单的listview活动,其中字符串数组从string.xml读取数组字符串,这会引发错误。我已经发布了下面的代码和日志,
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
答案 0 :(得分:4)
移动此
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 :(得分:-1)
我认为:
String[] presidents = getResources().getStringArray(R.array.android_os_flavours);
应该是:
String[] presidents = getResources().getStringArray(R.string.android_os_flavours);
因为它位于strings.xml文件中,而不在arrays.xml文件中