文件观察器 onEvent() 中的 startActivity() 出现问题
Trouble with startActivity() in file observers onEvent()
我有一个 fileObserver 运行,现在我需要在 onEvent 被调用后启动一个 activity,考虑到我无法获得,我不太确定该怎么做startActivity().
这是我当前的代码:
public class snapObserver extends FileObserver {
public final String TAG = "DEBUG";
public static Context mContext;
public snapObserver(String path) {
super(path, FileObserver.CREATE);
}
@Override
public void onEvent(int event, String path) {
if (path == null){
Log.d(TAG, "path is null");
}
Log.d(TAG, "File created!!!!");
mContext.startActivity(new Intent(mContext, editActivity.class));
}
}
此代码抛出 java.lang.NullPointerException
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.content.Context.getPackageName()' on a null
object reference
at android.content.ComponentName.(ComponentName.java:77)
at android.content.Intent.(Intent.java:3996)
at com.samplersnapshoot.domiq.samplersnapshoot.snapObserver.onEvent(snapObserver.java:28)
at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:122)
at android.os.FileObserver$ObserverThread.observe(Native Method)
at android.os.FileObserver$ObserverThread.run(FileObserver.java:85)
我在后台有一个服务 运行,它通过简单的 startWatching 调用 fileObserver。
尝试这样做:
public class snapObserver extends FileObserer {
private Context mContext;
//any code
public snapObserver(String path, Context context) {
super(path, FileObserver.CREATE);
this.mContext = context;
}
//any code
}
我有一个 fileObserver 运行,现在我需要在 onEvent 被调用后启动一个 activity,考虑到我无法获得,我不太确定该怎么做startActivity().
这是我当前的代码:
public class snapObserver extends FileObserver {
public final String TAG = "DEBUG";
public static Context mContext;
public snapObserver(String path) {
super(path, FileObserver.CREATE);
}
@Override
public void onEvent(int event, String path) {
if (path == null){
Log.d(TAG, "path is null");
}
Log.d(TAG, "File created!!!!");
mContext.startActivity(new Intent(mContext, editActivity.class));
}
}
此代码抛出 java.lang.NullPointerException
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ComponentName.(ComponentName.java:77) at android.content.Intent.(Intent.java:3996) at com.samplersnapshoot.domiq.samplersnapshoot.snapObserver.onEvent(snapObserver.java:28) at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:122) at android.os.FileObserver$ObserverThread.observe(Native Method) at android.os.FileObserver$ObserverThread.run(FileObserver.java:85)
我在后台有一个服务 运行,它通过简单的 startWatching 调用 fileObserver。
尝试这样做:
public class snapObserver extends FileObserer {
private Context mContext;
//any code
public snapObserver(String path, Context context) {
super(path, FileObserver.CREATE);
this.mContext = context;
}
//any code
}