Java 异步任务复制文件

Java 异步任务复制文件,java,android,Java,Android,我正在尝试使用AsyncTask将一些文件从一个文件夹复制到另一个文件夹,然后列出它们。然而,当我执行CopyandList时,我的应用程序只是强制关闭。如有任何建议,将不胜感激 这是我的异步任务代码 private class CopyandList extends AsyncTask<Void, Void, Void> { private ProgressDialog dialog; @Override protected void onPreExec

我正在尝试使用AsyncTask将一些文件从一个文件夹复制到另一个文件夹,然后列出它们。然而,当我执行CopyandList时,我的应用程序只是强制关闭。如有任何建议,将不胜感激

这是我的异步任务代码

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);
        }
    }
}

如果不查看
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
而没有初始化它。因此,LogCat中的
Null

代码

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

    }

当您调用
dialog.setMessage(“复制文件…”)时,您会得到一个
NullPointerException
。您需要首先初始化
对话框。

日志猫说什么?什么是
快照?另外,看起来您正在尝试使用一些根方法来复制文件,您正在测试的设备是否具有根访问权限?否则,可能会导致强制关闭。
对话框在哪里初始化?此外,堆栈跟踪将非常有用;我不知道输出会去哪里。改用Log类。我认为dialog=newprogressdialog(conetxt)????道歉,补充道logcat@user2415188请再看一下答案,相应地改变了。
@Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(Activity_Main.this, "Copying",
                "Please Wait.. ", true);

    }