我试图在设备启动时启动IntentService,但我一直收到此错误:
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: FATAL EXCEPTION: main
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: Process: fi.tut.cs.social.proximeety, PID: 2675
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start service fi.tut.cs.social.proximeety.N2YIntentService@2aab3021 with Intent { cmp=fi.tut.cs.social.proximeety/.N2YIntentService (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.app.IntentService$ServiceHandler.obtainMessage()' on a null object reference
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread.access$2100(ActivityThread.java:151)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.app.IntentService$ServiceHandler.obtainMessage()' on a null object reference
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.IntentService.onStart(IntentService.java:116)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.IntentService.onStartCommand(IntentService.java:130)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread.access$2100(ActivityThread.java:151)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-11 02:03:39.009 2675-2675/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fi.tut.cs.social.proximeety" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".N2YIntentService"
android:exported="true"/>
</application>
</manifest>
BootBroadcastReceiver.java
package fi.tut.cs.social.proximeety;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
public class BootBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Launch the specified service when this message is received
Intent startServiceIntent = new Intent(context, N2YIntentService.class);
context.startService(startServiceIntent);
}
}
N2YIntentService.java
package fi.tut.cs.social.proximeety;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
public class N2YIntentService extends IntentService{
final String TAG = "N2YIntentService";
// Default constructor
public N2YIntentService() {
super("N2YIntentService");
Log.d(TAG, "Default Constructor");
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate()");
}
@Override
public void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent()");
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
如果你能发现我的代码有问题,我真的很感激。 提前谢谢!
答案 0 :(得分:11)
在N2YIntentService中,您应该在onCreate方法中调用super.onCreate()
。