onListItemClick抛出异常(Android)

时间:2013-10-22 07:13:17

标签: java android exception onclick

我复制了一些代码并且它有效,但是当我尝试进行一些更改时会引发异常,但我不知道为什么,因为我不了解所有代码,也许有人可以帮助我:< / p>

MainActivity.java:

public class MainActivity extends ListActivity {
private File currentDir;
private FileArrayAdapter adapter;
private String SearchString = "test";
List<Option>dir = new ArrayList<Option>();
List<Option>fls = new ArrayList<Option>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    currentDir = new File("/sdcard/Test");
    fill(currentDir);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Log.d("TEST", adapter.getItem(position).getPath());//aktuellen Pfad ins Syslog schreiben
    Option o = adapter.getItem(position);

    onFileClick(o);
}

private void onFileClick(Option o)
{
    Log.d("TEST", o.getPath());//aktuellen Pfad ins Syslog schreiben
    File file = new File(o.getPath());
    Intent opdf = new Intent(Intent.ACTION_VIEW);
    opdf.setDataAndType(Uri.fromFile(file), "application/pdf");
    opdf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(opdf);
}

public static String removeExtension(String s) {

    String separator = System.getProperty("file.separator");
    String filename;

    // Remove the path upto the filename.
    int lastSeparatorIndex = s.lastIndexOf(separator);
    if (lastSeparatorIndex == -1) {
        filename = s;
    } else {
        filename = s.substring(lastSeparatorIndex + 1);
    }

    // Remove the extension.
    int extensionIndex = filename.lastIndexOf(".");
    if (extensionIndex == -1)
        return filename;

    return filename.substring(0, extensionIndex);
}

private void fill(File f)
{
    File[]dirs = f.listFiles();

     try{
         for(File ff: dirs)
         {
            if(ff.isDirectory()){
                File r = new File (ff.getPath());
                Log.d("TEST", ff.getPath());//aktuellen Pfad ins Syslog schreiben
                fill2(r);
            }
            else
            {

            }
         }
     }catch(Exception e)
     {

     }
     Collections.sort(dir);
     Collections.sort(fls);
     dir.addAll(fls);
     adapter = new FileArrayAdapter(MainActivity.this,R.layout.activity_main,dir);
     this.setListAdapter(adapter);

}
private void fill2(File f)
{
    File[]dirs = f.listFiles();

     try{
         for(File ff: dirs)
         {
             if(ff.isDirectory()){
                 fill2(ff);
             }
             else{
                 String Name = (ff.getName().toLowerCase(Locale.GERMAN));
                 if (Name.contains(SearchString.toLowerCase(Locale.GERMAN))){
                     fls.add(new Option(removeExtension(ff.getName()),"",null));
                 }
             }

         }
     }catch(Exception e)
     {

 }

 }

 }

FileArrayAdapter.java:

public class FileArrayAdapter extends ArrayAdapter<Option>{

private Context c;
private int id;
private List<Option>items;

public FileArrayAdapter(Context context, int textViewResourceId,
        List<Option> objects) {
    super(context, textViewResourceId, objects);
    c = context;
    id = textViewResourceId;
    items = objects;
}

public Option getItem(int i)
 {
     return items.get(i);
 }
 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
          if (v == null) {
              LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = vi.inflate(id, null);
          }
          final Option o = items.get(position);
          if (o != null) {
                  TextView t1 = (TextView) v.findViewById(R.id.TextView01);
                  TextView t2 = (TextView) v.findViewById(R.id.TextView02);

                  if(t1!=null)
                    t1.setText(o.getName());
                  if(t2!=null)
                    t2.setText(o.getData());
                //int drawableID = R.drawable.file_icon;

                //t1.setCompoundDrawablesWithIntrinsicBounds(drawableID, 0,
                //      0, 0);

                t1.setEllipsize(null);
                t1.setTextSize(54);
                t1.setBackgroundColor(Color.LTGRAY);
                t2.setBackgroundColor(Color.LTGRAY);
          }
          return v;
  }



}

Option.java:

public class Option implements Comparable<Option>{
private String name;
private String data;
private String path;

public Option(String n,String d,String p)
{
    name = n;
    data = d;
    path = p;
}
public String getName()
{
    return name;
}
public String getData()
{
    return data;
}
public String getPath()
{
    return path;
}
@Override
public int compareTo(Option o) {
    if(this.name != null)
        return this.name.toLowerCase().compareTo(o.getName().toLowerCase()); 
    else 
        throw new IllegalArgumentException();
}
}

logcat的:

E/AndroidRuntime(30173): FATAL EXCEPTION: main
E/AndroidRuntime(30173): java.lang.NullPointerException
E/AndroidRuntime(30173): at java.io.File.fixSlashes(File.java:185)
E/AndroidRuntime(30173): at java.io.File.<init>(File.java:134)
E/AndroidRuntime(30173): at com.example.test.MainActivity.onFileClick(MainActivity.java:47)
E/AndroidRuntime(30173): at com.example.test.MainActivity.onListItemClick(MainActivity.java:41)
E/AndroidRuntime(30173): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
E/AndroidRuntime(30173): at android.widget.AdapterView.performItemClick(AdapterView.java:297)
E/AndroidRuntime(30173): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
E/AndroidRuntime(30173): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2754)
E/AndroidRuntime(30173): at android.widget.AbsListView$1.run(AbsListView.java:3428)
E/AndroidRuntime(30173): at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(30173): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(30173): at android.os.Looper.loop(Looper.java:152)
E/AndroidRuntime(30173): at android.app.ActivityThread.main(ActivityThread.java:5132)
E/AndroidRuntime(30173): at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime(30173): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(30173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(30173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(30173): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 2112): Force finishing activity com.example.test/.MainActivity

2 个答案:

答案 0 :(得分:0)

我认为来自您的对象“选项”的“路径”是正确的。

此行导致崩溃:

File file = new File(o.getPath());

关于你的方法“onFileClick”。

尝试打印o.getPath()以查看他的值是什么,但我认为他可能是null。

答案 1 :(得分:0)

MainActivity fill2 方法中,您正在创建一个带有空路径的Option对象:

fls.add(new Option(removeExtension(ff.getName()),"",null));

然后你正在打电话:

File file = new File(o.getPath());

抛出NullPointerException。

将以上行更改为:

fls.add(new Option(removeExtension(ff.getName()),"",ff.getPath()));