我有一个pasteFile类,只是将文件粘贴到某个目录。但是当我将文件粘贴到根目录时,文件会被粘贴,但应用程序强制因NullPointerException而关闭。这是我的课程代码:
@Override
protected List<String> doInBackground(String... content) {
final List<String> failed = new ArrayList<String>();
final Activity activity = this.activity.get();
for (String target : content) {
if (ClipBoard.isMove()) {
SimpleUtils.moveToDirectory(activity, target, location);
success = true;
} else {
SimpleUtils.copyToDirectory(target, location);
success = true;
}
}
SimpleUtils.requestMediaScanner(activity,
new File(location).listFiles());
return failed;
}
以及实际导致问题的requestMediaScanner():
public static void requestMediaScanner(final Context context,
final File... files) {
final String[] paths = new String[files.length];
int i = 0;
for (final File file : files) {
paths[i] = file.getPath();
i++;
}
MediaScannerConnection.scanFile(context, paths, null, null);
}
任何想法我做错了什么?
编辑:添加了logcat
Process: com.tproductions.Openit, PID: 25526
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.tproductions.Openit.utils.SimpleUtils.requestMediaScanner(SimpleUtils.java:33)
at com.tproductions.Openit.tasks.PasteTask.doInBackground(PasteTask.java:78)
at com.tproductions.Openit.tasks.PasteTask.doInBackground(PasteTask.java:22)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
01-30 16:57:11.158 25526-25526/com.tproductions.Openit E/WindowManager﹕ android.view.WindowLeaked: Activity com.tproductions.Openit.Browser has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42ee8950 V.E..... R......D 0,0-684,192} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:294)
at com.tproductions.Openit.tasks.PasteTask.onPreExecute(PasteTask.java:58)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.tproductions.Openit.tasks.PasteTaskExecutor.next(PasteTaskExecutor.java:107)
at com.tproductions.Openit.tasks.PasteTaskExecutor.start(PasteTaskExecutor.java:57)
at com.tproductions.Openit.Browser.onOptionsItemSelected(Browser.java:498)
at android.app.Activity.onMenuItemSelected(Activity.java:2600)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1019)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:119)
at android.view.View.performClick(View.java:4569)
at android.view.View$PerformClick.run(View.java:18553)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我把scan方法放到if语句中,现在它没有给出任何错误。问题是变量files
有时因为根文件夹中的一些奇怪而为空。但现在,它正常运作。以下是工作代码段:
public static void requestMediaScanner(final Context context,
final File... files) {
if(files!=null){
String[] paths = new String[files.length];
int i = 0;
for (final File file : files) {
paths[i] = file.getPath();
Log.e("tag", paths[i]);
i++;
}
MediaScannerConnection.scanFile(context, paths, null, null);
}
}