2013-06-15 18 views
6

Próbowałem utworzyć działanie, które uruchamia usługę (w nowym wątku). Składa się z układu z dwoma editText i przyciskiem o nazwie Send. Chodzi o to, że kiedy wykonać główną działalność, to rzuca, że:java.lang.RuntimeException nie może wykonać instancji usługi: java.lang.NullPointerException

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 

Kod działalności podstawowej to:

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; 
    } 
} 

kod od NotificationsService jest tutaj:

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) 
    } 
} 

i AndroidManifest to:

<?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> 
+0

nie można wywołać metody getSystemService przed wywołaniem funkcji onStart, ponieważ kontekst nie został zainicjowany. – njzk2

+0

@ njzk2 dzięki za to! Zmieniłem go, ale nadal nie działa:/ – Mitodina

+0

Poinformuj o nowym śledzeniu stosu, ponieważ zawieszasz się teraz gdzie indziej. – CommonsWare

Odpowiedz

10

Nie można wywołać getSystemService() z inicjalizatora (jak pierwotnie byłeś) lub z konstruktora (tak jak teraz). Zastąp numer onCreate() i zadzwoń pod numer getSystemService().

Zasadniczo, aż onCreate() nazywa się (lub, bardziej precyzyjnie, aż po wywołaniu super.onCreate() ze swojego onCreate()), odziedziczone metody na Service nie są jeszcze gotowe do użytku.

+0

wielkie dzięki! Zadziałało! :) – Mitodina

2

Miałem ten sam błąd. Mój błąd polegał na tym, że usługa nie miała pustego domyślnego konstruktora. Oto kod, który go rozwiązał:

public class MyService extends IntentService { 

    public MyService() { 
     super(null); // That was the lacking constructor 
    } 

    public MyService(String name) { 
     super(name); 
    } 

    //... 

}