06-15 22:32:13.312: E/AndroIDRuntime(643): FATAL EXCEPTION: main06-15 22:32:13.312: E/AndroIDRuntime(643): java.lang.RuntimeException: Unable to instantiate service hikoki.services.NotificationsService: java.lang.NullPointerException06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.app.ActivityThread.handleCreateService(ActivityThread.java:2237)06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.app.ActivityThread.access00(ActivityThread.java:123)06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.os.Handler.dispatchMessage(Handler.java:99)06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.os.Looper.loop(Looper.java:137)06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.app.ActivityThread.main(ActivityThread.java:4424)06-15 22:32:13.312: E/AndroIDRuntime(643): at java.lang.reflect.Method.invokeNative(Native Method)06-15 22:32:13.312: E/AndroIDRuntime(643): at java.lang.reflect.Method.invoke(Method.java:511)06-15 22:32:13.312: E/AndroIDRuntime(643): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)06-15 22:32:13.312: E/AndroIDRuntime(643): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)06-15 22:32:13.312: E/AndroIDRuntime(643): at dalvik.system.NativeStart.main(Native Method)06-15 22:32:13.312: E/AndroIDRuntime(643): Caused by: java.lang.NullPointerException06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.content.Contextwrapper.getSystemService(Contextwrapper.java:386)06-15 22:32:13.312: E/AndroIDRuntime(643): at hikoki.services.NotificationsService.<init>(NotificationsService.java:35)06-15 22:32:13.312: E/AndroIDRuntime(643): at java.lang.class.newInstanceImpl(Native Method)06-15 22:32:13.312: E/AndroIDRuntime(643): at java.lang.class.newInstance(Class.java:1319)06-15 22:32:13.312: E/AndroIDRuntime(643): at androID.app.ActivityThread.handleCreateService(ActivityThread.java:2234)06-15 22:32:13.312: E/AndroIDRuntime(643): ... 10 more
主要活动的代码是这样的:
public class MainActivity extends Activity { private String TAG = getClass().getSimplename(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Thread t= new Thread(){ public voID run(){ Intent intent=new Intent(getContext(),NotificationsService.class); //Intent intent=new Intent("hikoki.services.NotificationsService"); Log.d(TAG,"sending notificationsService"); startService(intent); } }; t.start(); } protected Context getContext() { return this; }}
NotificationsService代码在这里:
public class NotificationsService extends IntentService{ private static int TIME_INTERVAL_MIliS=5000; private static final int REFRESH=10; private notificationmanager noteManager; private static List<FlightStatusNote> flights= new ArrayList<FlightStatusNote>(); public NotificationsService(){ super("NotificationsService"); noteManager= notificationmanager)getSystemService(Context.NOTIFICATION_SERVICE) }}
AndroIDManifest是这样的:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="hikoki.services" androID:versionCode="1" androID:versionname="1.0" ><uses-sdk androID:minSdkVersion="15" androID:targetSdkVersion="17" /><uses-permission androID:name="androID.permission.INTERNET" /><uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" /><application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="hikoki.services.MainActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <service androID:name=".FlightStatusService"></service> <service androID:enabled="true" androID:name=".NotificationsService"></service></application></manifest>解决方法 您不能从初始化程序(如您原来的那样)或构造函数(就像您现在一样)调用getSystemService().请覆盖onCreate(),并在那里调用getSystemService().
基本上,直到调用onCreate()(或者更准确地说,直到你从onCreate()调用super.onCreate()之后),服务上的继承方法还没有准备好供您使用.
总结以上是内存溢出为你收集整理的android – java.lang.RuntimeException无法实例化服务:java.lang.NullPointerException全部内容,希望文章能够帮你解决android – java.lang.RuntimeException无法实例化服务:java.lang.NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)