AsyncTask复制文件

时间:2013-05-23 20:56:21

标签: java android

我正在尝试使用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();

道歉,这是我的logcat。

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

2 个答案:

答案 0 :(得分:2)

致电NullPointerException时收到dialog.setMessage("Copying Files ...");。您需要先初始化dialog

答案 1 :(得分:0)

如果不查看LogCat消息,很难猜出问题。您仍然可以尝试使用此代码复制文件

private void FileMoving(String inputFile, String outputFile) {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(inputFile);
        out = new FileOutputStream(outputFile);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

        // write the output file (You have now copied the file)
        out.flush();
        out.close();
        out = null;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

添加LogCat后编辑 您的onPreExecute()似乎在ProgressDialog使用Null,而不会将其初始化。因此LogCat中的@Override protected void onPreExecute() { progress = ProgressDialog.show(Activity_Main.this, "Copying", "Please Wait.. ", true); }

代码

{{1}}