2012-02-15 48 views
1

debug.java致命異常:主要在程序中搜索PDF文件的SD卡

package com.himanshu; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.util.Log; 

    public class BookShelfDebugActivity extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      Log.i("debug","qwert111111"); 

      FileWalker fw=new FileWalker(); 
      fw.Walker("/"); 
      String a=fw.ListPDF[0].getName(); 
      String b=fw.ListPDF[1].getName(); 

      Log.i("debug",a); 
        Log.i("debug",b); 



     } 
    } 

FileWalker.java

package com.himanshu; 

    import java.lang.String; 
    import java.io.File; 
    import android.app.ProgressDialog; 
    import android.content.Context; 


    public class FileWalker { 

     public File[] ListPDF; 
     public int count = 0; 
     public String Path; 
     public Context context; 

    public void Walker(String Path){ 
      File root = new File(Path); 
      File [] List = root.listFiles(); 
      for(File f :List){ 
       if(f.isDirectory()){ 
        Walker(f.getAbsolutePath()); 
       } 
       else{ 
        if(f.getName().endsWith(".pdf")){ 
         ListPDF[count] = f; 
         count++; 
        } 
       } 
      } 
     } 
    } 

錯誤

02-15 20:05:26.903: E/AndroidRuntime(1313): FATAL EXCEPTION: main 
02-15 20:05:26.903: E/AndroidRuntime(1313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.himanshu/com.himanshu.BookShelfDebugActivity}: java.lang.NullPointerException 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.os.Looper.loop(Looper.java:123) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at dalvik.system.NativeStart.main(Native Method) 
02-15 20:05:26.903: E/AndroidRuntime(1313): Caused by: java.lang.NullPointerException 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at com.himanshu.FileWalker.Walker(FileWalker.java:35) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at com.himanshu.FileWalker.Walker(FileWalker.java:37) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at com.himanshu.BookShelfDebugActivity.onCreate(BookShelfDebugActivity.java:16) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-15 20:05:26.903: E/AndroidRuntime(1313):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 

我只是試圖找到SD卡中的所有PDF文件..但程序不工作... 我複製了兩個PDF文件從桌面到AVD的/ data/mnt/sdcard並試圖在logcat上打印它們的名字。 請幫我解決...

+0

您是否向AndroidManifest添加了適當的權限? 我在想android.permission.WRITE_EXTERNAL_STORAGE。 – 2012-02-15 15:10:33

+0

您應該閱讀[Java命名約定](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367)。你的代碼有點難以閱讀。 – nfechner 2012-02-15 15:25:54

回答

3

好吧,有一件事情會跳出來。你聲明一個數組,但從不分配數組,然後將項分配給數組。

public File[] ListPDF; 

... 

ListPDF[count] = f; 

每次都會崩潰。由於這是一個不受限制的項目列表,ArrayList會更好。

List<File> ListPDF = new ArrayList<File>(); 

... 

ListPDF.add(f); 
+0

thanx很多爲你的建議... :) – 2012-02-16 13:45:00

+0

最受歡迎,祝你好運。 – 2012-02-16 13:57:17

1

使用:

File [] List = root.listFiles(); 
if(List!=null) 
{ 
    for(File f :List) 
    { 
     /* your stuff */ 
    } 
} 

也,一定要允許android.permission.WRITE_EXTERNAL_STORAGE添加到您的清單文件。