尝试以编程方式复制文件时强制关闭 - Android / Java

时间:2014-01-09 21:00:14

标签: java android copy fatal-error forceclose

我正在尝试使用以下方式以编程方式复制文件:

public void function1(int id) {
    Toast.makeText(this, "Copy", Toast.LENGTH_SHORT).show();

    try {
        copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


public static void copyDirectoryOneLocationToAnotherLocation(
        File sourceLocation, File targetLocation) throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < sourceLocation.listFiles().length; i++) {

            copyDirectoryOneLocationToAnotherLocation(new File(
                    sourceLocation, children[i]), new File(targetLocation,
                    children[i]));
        }
    } else {

        InputStream in = new FileInputStream(sourceLocation);

        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

}

但是我收到错误声明:

FATAL EXCEPTION: main NullPointerException at com.example.project.FileChooser.function1(FileChooser.java:164)

就行:

copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);

我想我可能使用了copyDirectoryOneLocationToAnotherLocation中传递的错误参数

我想也许我应该使用:sourceLocation,targetLocation但这对我来说也不起作用。

logcat的:

01-09 15:54:49.026: E/AndroidRuntime(15378): FATAL EXCEPTION: main
01-09 15:54:49.026: E/AndroidRuntime(15378): java.lang.NullPointerException
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.copyDirectoryOneLocationToAnotherLocation(FileChooser.java:205)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.copyDirectoryOneLocationToAnotherLocation(FileChooser.java:207)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.function1(FileChooser.java:164)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.onContextItemSelected(FileChooser.java:148)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.app.Activity.onMenuItemSelected(Activity.java:2597)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3663)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:963)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AbsListView$1.run(AbsListView.java:3571)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.os.Handler.handleCallback(Handler.java:725)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.os.Looper.loop(Looper.java:153)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.app.ActivityThread.main(ActivityThread.java:5297)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at java.lang.reflect.Method.invoke(Method.java:511)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

如果您在此行中获得NullPointerException

copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);

这意味着currentDir为空。我没有看到您将其分配给代码中的任何内容,因此将其分配给目录,然后它将不再为null。