单击列表视图项时出现错误java.lang.NullPointerException

时间:2013-10-11 06:51:42

标签: android file android-listview nullpointerexception

我必须在列表视图中显示已创建的文件。我通过下面的代码获取这些保存的文件,并用这些文件填充列表视图,

String[] SavedFiles = getApplicationContext().fileList();
          if(SavedFiles != null)
          {
              for(int i=0; i<SavedFiles.length; i++)
              {
                  arrList.add(SavedFiles[i]);
              }

              for(int l=0; l<arrList.size(); l++)
              {
                  if(arrList.get(l).contains("New"))
                  {
                      arrList.remove("New");
                  }
              }
          }
           ArrayAdapter<String> adapterSaveFiles = new ArrayAdapter<String>(this, R.layout.text, arrList);
           lv.setAdapter(adapterSaveFiles);
           adapterSaveFiles.notifyDataSetChanged();

现在,当我单击列表视图的项目以查看文件的内容时,它首先工作2-3次,但是当我点击列表项时,我收到以下错误...

10-11 06:45:01.759: E/AndroidRuntime(1085): FATAL EXCEPTION: main
10-11 06:45:01.759: E/AndroidRuntime(1085): java.lang.NullPointerException
10-11 06:45:01.759: E/AndroidRuntime(1085):     at java.util.ArrayList.addAll(ArrayList.java:184)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at com.example.retailrun.NewFriendList$3.onItemClick(NewFriendList.java:211)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.os.Handler.handleCallback(Handler.java:725)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.os.Looper.loop(Looper.java:137)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at java.lang.reflect.Method.invoke(Method.java:511)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-11 06:45:01.759: E/AndroidRuntime(1085):     at dalvik.system.NativeStart.main(Native Method)
10-11 06:45:01.929: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
10-11 06:45:02.729: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

要解决这个问题需要做些什么?

点击列表项时的代码

FileInputStream fisRead =null;
ArrayList<String> existing; 
        try 
        {
            fisRead = openFileInput(fileName);
        } catch (FileNotFoundException e3) {

            e3.printStackTrace();
        }
        Scanner scan = new Scanner(new DataInputStream(fisRead));
        existing = new ArrayList<String>();
        existing.clear();

        while (scan.hasNextLine())
        {
            String name = scan.next();
            if(scan.hasNext())
            {
                name = String.format("%s %s", name.trim(), scan.nextLine().trim());
            }
             existing.add(name);

        }

        scan.close();


        adapterFriends = new ArrayAdapter<String>(getBaseContext(),R.layout.checklist, existing);
        lvFinal.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvFinal.setAdapter(adapterFriends);
        adapterFriends.notifyDataSetChanged();

0 个答案:

没有答案