debug.java
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 :(得分:3)
好吧,有一件事情跳了出来。您正在声明一个数组,但从不分配数组,然后将项目分配给数组。
public File[] ListPDF;
...
ListPDF[count] = f;
每次都会相当严重。由于这是一个无限的项目列表,ArrayList
会更好。
List<File> ListPDF = new ArrayList<File>();
...
ListPDF.add(f);
答案 1 :(得分:1)
使用:
File [] List = root.listFiles();
if(List!=null)
{
for(File f :List)
{
/* your stuff */
}
}
另外,请务必在清单文件中添加权限android.permission.WRITE_EXTERNAL_STORAGE
。