我有以下代码:
PreferenceScreen root = this.getPreferenceScreen();
ListPreference lp = (ListPreference) root.findPreference("signedUpCompetetion");
CharSequence[] entries = null;
CharSequence[] entryValues = null;
FileInputStream fis;
try {
fis = openFileInput("list_file.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
entries = new CharSequence[10];
entryValues = new CharSequence[10];
int i = 0;
while ((line = bufferedReader.readLine()) != null) {
entries[i] = line;
entryValues[i] = ""+i;
i++;
Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
我从文件中读取然后将其添加到位置的数组中
但是,当我尝试打开我的 listpreference 时,我得到以下 nullpointer exepection:
05-26 00:24:13.867: E/AndroidRuntime(1774): FATAL EXCEPTION: main
05-26 00:24:13.867: E/AndroidRuntime(1774): java.lang.NullPointerException
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.ListPreference.findIndexOfValue(ListPreference.java:215)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.ListPreference.getValueIndex(ListPreference.java:224)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.ListPreference.getEntry(ListPreference.java:202)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.ListPreference.getSummary(ListPreference.java:148)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.Preference.onBindView(Preference.java:522)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.Preference.getView(Preference.java:460)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.ListView.makeAndAddView(ListView.java:1831)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.ListView.fillDown(ListView.java:674)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.ListView.fillFromTop(ListView.java:735)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.ListView.layoutChildren(ListView.java:1652)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.View.layout(View.java:14003)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewGroup.layout(ViewGroup.java:4375)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.View.layout(View.java:14003)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewGroup.layout(ViewGroup.java:4375)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.View.layout(View.java:14003)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewGroup.layout(ViewGroup.java:4375)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.View.layout(View.java:14003)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewGroup.layout(ViewGroup.java:4375)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.View.layout(View.java:14003)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewGroup.layout(ViewGroup.java:4375)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.Choreographer.doFrame(Choreographer.java:532)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.os.Handler.handleCallback(Handler.java:725)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.os.Looper.loop(Looper.java:137)
05-26 00:24:13.867: E/AndroidRuntime(1774): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-26 00:24:13.867: E/AndroidRuntime(1774): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 00:24:13.867: E/AndroidRuntime(1774): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 00:24:13.867: E/AndroidRuntime(1774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-26 00:24:13.867: E/AndroidRuntime(1774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-26 00:24:13.867: E/AndroidRuntime(1774): at dalvik.system.NativeStart.main(Native Method)
谁能告诉我为什么会这样?
请注意,我的 Toast 消息显示正确的输出。