1

我正在尝试使用 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
4

2 回答 2

2

NullPointerException当你打电话时你会得到一个dialog.setMessage("Copying Files ...");。您需要初始化第dialog一个。

于 2013-05-25T00:29:25.763 回答
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而不初始化它。因此NullLogCat 中的值

代码

@Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(Activity_Main.this, "Copying",
                "Please Wait.. ", true);

    }
于 2013-05-23T21:05:46.493 回答