我正在尝试使用 AsyncTask 将一些文件从一个文件夹复制到另一个文件夹,然后列出它们。但是,当我执行 CopyandList 时,我的应用程序只是强制关闭。任何意见,将不胜感激。
这是我的 AsyncTask 代码
private class CopyandList extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog.setMessage("Copying Files ...");
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
File file;
file = new File(Snapdes);
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
String x=(directory + list[i].getName());
String y=(Snapdes);
if(list[i].equals(y)){
}else{
RootTools.copyFile(x, y, false, true);
}
}
return null;
}
protected void onPostExecute(Void result) {
if (dialog.isShowing()) {
dialog.dismiss();
File file;
file = new File(Snapdes);
File list[] = file.listFiles();
ListView ListView = (ListView) findViewById(R.id.SnapList);
List<String> SnapList;
SnapList = new ArrayList<String>();
for( int i=0; i< list.length; i++){
SnapList.add( list[i].getName() );
System.out.print(list[i]);
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (MainActivity.this,
android.R.layout.simple_list_item_1,
android.R.id.text1, SnapList);
ListView.setAdapter(adapter);
}
}
}
然后我在主要活动中执行它
new CopyandList().execute();
抱歉,这是我的日志。
05-23 21:26:56.145: D/AndroidRuntime(24915): Shutting down VM
05-23 21:26:56.145: W/dalvikvm(24915): threadid=1: thread exiting with uncaught exception (group=0x41770930)
05-23 21:26:56.165: E/AndroidRuntime(24915): FATAL EXCEPTION: main
05-23 21:26:56.165: E/AndroidRuntime(24915): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itsallwhite.sneakysnapper/com.itsallwhite.sneakysnapper.MainActivity}: java.lang.NullPointerException
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.ActivityThread.access$600(ActivityThread.java:153)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.os.Looper.loop(Looper.java:137)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.ActivityThread.main(ActivityThread.java:5226)
05-23 21:26:56.165: E/AndroidRuntime(24915): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 21:26:56.165: E/AndroidRuntime(24915): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 21:26:56.165: E/AndroidRuntime(24915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-23 21:26:56.165: E/AndroidRuntime(24915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-23 21:26:56.165: E/AndroidRuntime(24915): at com.android.internal.os.ZygoteInit.main(Native Method)
05-23 21:26:56.165: E/AndroidRuntime(24915): at dalvik.system.NativeStart.main(Native Method)
05-23 21:26:56.165: E/AndroidRuntime(24915): Caused by: java.lang.NullPointerException
05-23 21:26:56.165: E/AndroidRuntime(24915): at com.itsallwhite.sneakysnapper.MainActivity$CopyandList.onPreExecute(MainActivity.java:119)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.os.AsyncTask.execute(AsyncTask.java:534)
05-23 21:26:56.165: E/AndroidRuntime(24915): at com.itsallwhite.sneakysnapper.MainActivity.onCreate(MainActivity.java:66)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.Activity.performCreate(Activity.java:5104)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-23 21:26:56.165: E/AndroidRuntime(24915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
05-23 21:26:56.165: E/AndroidRuntime(24915): ... 12 more