Reading from file and add to list preference

风格不统一 提交于 2020-01-15 12:32:21

问题


I have the following code:

        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);

Where i read from the file and then add it to an array at the possition

However when i try to open my listpreference i get the following 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)

Can anyone tell me why this is happening?

Please note that my Toast message display the correct output.

来源:https://stackoverflow.com/questions/16755156/reading-from-file-and-add-to-list-preference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!