gpt4 book ai didi

java - FileObserver 中的 NullPointerException - Android

转载 作者:行者123 更新时间:2023-12-01 14:06:32 27 4
gpt4 key购买 nike

我现在正在编写 FileObserver 代码。我有以下代码,如果运行它,我将收到 NullPointerException。

public class FileModificationService extends Service{
private MyFileObserver fileOb;
private static final int MAX_FO = 1;
private List<MyFileObserver> fileOb_list = new ArrayList<MyFileObserver>();
@Override
public void onCreate() {
if (!EnvironmentUtilsStatic.is_external_storage_available()) {
Toast.makeText(FileModificationService.this, "SDCARD is not available!", Toast.LENGTH_SHORT).show();
return;
}
File sdcard = new File("/storage/sdcard/DCIM/Camera/SAMPLE IMAGES/");
if (sdcard == null) {
return;
} else {
fileOb_list.clear();
num_of_fos = 0;
createFileObs(sdcard);
}
}

//only create fileobserver for folders
int num_of_fos = 0;
private void createFileObs(File f) {
if (num_of_fos > MAX_FO) {
return;
}
if (!f.isDirectory()) {
MyFileObserver aFileOb = new MyFileObserver(f.getAbsolutePath());
fileOb_list.add(aFileOb);
} else {
fileOb = new MyFileObserver(f.getAbsolutePath());
MyFileObserver aFileOb = new MyFileObserver(f.getAbsolutePath());
fileOb_list.add(aFileOb);
num_of_fos++;
try {
for (File currentFile : f.listFiles()) {
createFileObs(currentFile);
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
}

@Override
public void onStart(Intent intent, int startid) {
fileOb.startWatching();
fileOb = new MyFileObserver("/storage/sdcard/DCIM/Camera/SAMPLE IMAGES/");// added code
for (int i = 0; i < fileOb_list.size(); ++i) { //This is line no. 60
fileOb_list.get(i).startWatching();
}
Toast.makeText(this.getApplicationContext(), "start monitoring file modification", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
fileOb.stopWatching();
for (int i = 0; i < fileOb_list.size(); ++i) {
fileOb_list.get(i).stopWatching();
}
Toast.makeText(this.getApplicationContext(), "stop monitoring file modification", Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

我收到以下 logcat -

09-17 13:05:55.915: E/AndroidRuntime(2596): FATAL EXCEPTION: main
09-17 13:05:55.915: E/AndroidRuntime(2596): java.lang.RuntimeException: Unable to start service roman10.tutorial.fileobserver.FileModificationService@b11293c0 with Intent { cmp=roman10.tutorial.fileobserver/.FileModificationService }: java.lang.NullPointerException
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2721)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.access$1900(ActivityThread.java:141)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.os.Looper.loop(Looper.java:137)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-17 13:05:55.915: E/AndroidRuntime(2596): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 13:05:55.915: E/AndroidRuntime(2596): at java.lang.reflect.Method.invoke(Method.java:525)
09-17 13:05:55.915: E/AndroidRuntime(2596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-17 13:05:55.915: E/AndroidRuntime(2596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 13:05:55.915: E/AndroidRuntime(2596): at dalvik.system.NativeStart.main(Native Method)
09-17 13:05:55.915: E/AndroidRuntime(2596): Caused by: java.lang.NullPointerException
09-17 13:05:55.915: E/AndroidRuntime(2596): at roman10.tutorial.fileobserver.FileModificationService.onStart(FileModificationService.java:60)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.Service.onStartCommand(Service.java:450)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2704)
09-17 13:05:55.915: E/AndroidRuntime(2596): ... 10 more

这段代码之前实际上是有效的。我没有对代码进行任何更改。但是,当我尝试运行此代码时,将模拟器从 Google 更改为 Intel x86 后,问题就出现了。但是,我尝试将模拟器转换回 Google。而且,该代码不起作用。

我不明白,为什么这段代码不起作用。

请指导我,提供一些建议,如果可能的话,提供教程。提前致谢。

最佳答案

onStart()fileOb.startWatching()中使用fileOb之前需要先初始化它

关于java - FileObserver 中的 NullPointerException - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18856249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com