android – java.lang.RuntimeException无法实例化服务:java.lang.NullPointerException

我试图创建一个启动服务的活动(在新线程中).它包含一个带有两个editText和一个名为Send的按钮的布局.关键是当我执行MainActivity时,它会抛出:
06-15 22:32:13.312: E/AndroidRuntime(643): FATAL EXCEPTION: main
06-15 22:32:13.312: E/AndroidRuntime(643): java.lang.RuntimeException: Unable to instantiate service hikoki.services.NotificationsService: java.lang.NullPointerException
06-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.access$1600(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.NullPointerException
06-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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...