在Android中实例化Intent Service时出现java.lang.NullPointerException

时间:2015-08-12 16:28:30

标签: android

我有一个自定义库,需要定期将文件上传到我的服务器(以及其他任务)。所以我在我的库中为它创建了一个Intent服务,我调用如下(注意,这不是任何活动的一部分,但是我为它创建了一个新线程并且我提供了上下文。所有日志消息到此为止得到印刷):

Intent logIntent = new Intent(mContext,LogIntentService.class);
logIntent.setAction("abc.xyz.org.mycustomlibrary.action.LOG");
File file = new File(mContext.getCacheDir(),localCache);
logIntent.putExtra(localCache, Uri.fromFile(file));
Log.d(TAG,"Log Intent Service invoked for file: " + file.toString()
+ " Cache Dir ="+mContext.getCacheDir()+ " Extra = " +
Uri.fromFile(file));
mContext.startService(logIntent);

明显的变化是:

 <!--[START log_intentserver] -->
    <service
        android:name="org.mypackagename.LogIntentService"
        android:exported="false"></service>
 <!--[END log_intentserver] -->

在我的LogIntentService中我有:

@Override
protected void onHandleIntent(Intent intent) {
    Log.d("On Handle of LogIntent");
    if (intent != null) {
        final String action = intent.getAction();
        if(ACTION_LOG.equals(action)) {
        ........
        }
     }
}
 public LogIntentService() {
    super(LogIntentService.class.getName());
 }

在我的adb logcat上,我找到了给出的代码片段的日志消息,如下所示:

  

为文件调用的Log Intent Service:/data/data/package.name/cache/LocalCache0 Cache Dir = / data / data / package.name.demo / cache Extra = file:/// data / data / package。名称/缓存/ LocalCache0

,错误信息为:

W/dalvikvm( 2046): threadid=1: thread exiting with uncaught exception (group=0x41611ba8)
E/AndroidRuntime( 2046): FATAL EXCEPTION: main
E/AndroidRuntime( 2046): Process: package.name, PID: 2046
E/AndroidRuntime( 2046): java.lang.RuntimeException: Unable to instantiate service abc.xyz.org.mycustomlibrary.LogIntentService: java.lang.NullPointerException
E/AndroidRuntime( 2046):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2543)
E/AndroidRuntime( 2046):    at android.app.ActivityThread.access$1800(ActivityThread.java:135)
E/AndroidRuntime( 2046):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime( 2046):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 2046):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 2046):    at android.app.ActivityThread.main(ActivityThread.java:5001)
E/AndroidRuntime( 2046):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2046):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 2046):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime( 2046):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime( 2046):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2046): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 2046):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
E/AndroidRuntime( 2046):    at abc.xyz.org.mycustomlibrary.LogIntentService.<init>(LogIntentService.java:33)
E/AndroidRuntime( 2046):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 2046):    at java.lang.Class.newInstance(Class.java:1208)
E/AndroidRuntime( 2046):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2540)
E/AndroidRuntime( 2046):    ... 10 more

我看过很多类似的帖子,但都没有帮助我。我在这里错过了什么吗?我对Android的意图比较陌生,所以我也可以忽略一些重要的细节。

0 个答案:

没有答案