私は次の問題に取り組んでいます:私はこれでサービスを開始しています:
Intent startIntent = new Intent(this, GPSService.class);
startService(startIntent);
サービスのコード:
package com.example.eapp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
public class GPSService extends Service implements LocationListener {
public final static int NOTIFICATION_ID = 1;
private String team = getString(R.string.team);
private String server = "http://server.com";
private int updateRate = 0;
private int updateDistance = 0;
private NotificationManager notifyManager;
private LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
final boolean gpsEnabled = getLocationManager().isProviderEnabled(
LocationManager.GPS_PROVIDER);
if (gpsEnabled) {
showIcon();
requestLocationUpdates();
} else {
Toast.makeText(this, R.string.gpsDisabled, Toast.LENGTH_SHORT)
.show();
stopSelf();
}
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
getLocationManager().removeUpdates(this);
notifyManager.cancel(NOTIFICATION_ID);
}
@Override
public void onLocationChanged(Location location) {
String url = server + "/setUserPosition.php" + "?latitude="
+ location.getLatitude() + "&longitude="
+ location.getLongitude() + "&dir=" + location.getBearing()
+ "&speed=" + location.getSpeed() + "&alt="
+ location.getAltitude() + "&accuracy="
+ location.getAccuracy() + "&user=" + team;
new GetTask().execute(url);
}
@Override
public void onProviderDisabled(String provider) {
// stopSelf();
}
@Override
public void onProviderEnabled(String provider) {
// requestLocationUpdates();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
private void requestLocationUpdates() {
if (locationManager != null)
locationManager.removeUpdates(this);
// get location service
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = getLocationManager().getBestProvider(crit, true);
getLocationManager().requestLocationUpdates(bestProvider,
updateRate * 1000, updateDistance, this);
}
private LocationManager getLocationManager() {
if (this.locationManager == null)
this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
return this.locationManager;
}
private void showIcon() {
notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, TrackActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.status_bar_message, team))
.setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
notifyManager.notify(NOTIFICATION_ID, notification);
}
}
サービスを開始しようとすると、次の例外が発生します。
03-29 21:22:21.688: E/AndroidRuntime(17548): FATAL EXCEPTION: main
03-29 21:22:21.688: E/AndroidRuntime(17548): java.lang.RuntimeException: Unable to instantiate service com.example.eapp.GPSService: java.lang.NullPointerException
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2538)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.app.ActivityThread.access$1600(ActivityThread.java:139)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.os.Looper.loop(Looper.java:154)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.app.ActivityThread.main(ActivityThread.java:4945)
03-29 21:22:21.688: E/AndroidRuntime(17548): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:22:21.688: E/AndroidRuntime(17548): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 21:22:21.688: E/AndroidRuntime(17548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-29 21:22:21.688: E/AndroidRuntime(17548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-29 21:22:21.688: E/AndroidRuntime(17548): at dalvik.system.NativeStart.main(Native Method)
03-29 21:22:21.688: E/AndroidRuntime(17548): Caused by: java.lang.NullPointerException
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.content.Context.getString(Context.java:283)
03-29 21:22:21.688: E/AndroidRuntime(17548): at com.jhquadrat.rauscher24.GPSService.<init>(GPSService.java:21)
03-29 21:22:21.688: E/AndroidRuntime(17548): at java.lang.Class.newInstanceImpl(Native Method)
03-29 21:22:21.688: E/AndroidRuntime(17548): at java.lang.Class.newInstance(Class.java:1319)
03-29 21:22:21.688: E/AndroidRuntime(17548): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2535)
03-29 21:22:21.688: E/AndroidRuntime(17548): ... 10 more
この例外がどこから来ているのか、それを取り除くために私がしなければならないことを誰か知っていますか?