새 스레드에서 서비스를 시작하는 활동을 만들려고했습니다. 두 개의 editText와 Send 버튼이있는 레이아웃으로 구성됩니다.java.lang.RuntimeException 서비스를 인스턴스화 할 수 없습니다. java.lang.NullPointerException
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)
}
}
이
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
주요 활동의 코드는 이것이다 : 요점은 내가 MainActivity을 실행할 때, 그것은 그 발생한다는 것입니다
및 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>
컨텍스트가 초기화되지 않았기 때문에 onStart가 호출되기 전에 getSystemService를 호출 할 수 없습니다. – njzk2
@ njzk2 감사합니다! 변경했지만 아직 작동하지 않습니다./ – Mitodina
다른 곳에서 충돌 할 때 새로운 스택 추적을 게시하십시오. – CommonsWare