我正在处理以下问题: 我正在用这个开始服务:
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
有没有人知道这个例外的来源,以及我可以做些什么来摆脱它?
答案 0 :(得分:4)
查看堆栈跟踪以查找最后一个“由以下原因:”消息。这是触发整个输出的异常。这是通过在您的代码的第21行调用NullPointerException
生成的getString
:
private String team = getString(R.string.team);
问题是,在为您的服务调用getString()
之前,您无法致电onCreate
。在此之前,框架尚未设置将服务代码链接到应用程序资源的结构。只需覆盖onCreate
并在那里设置team
。
. . .
private String team;
. . .
@Override
public void onCreate() {
team = getString(R.string.team);
}