0

我正在处理以下问题:我正在为此启动服务:

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

有谁知道这个异常来自哪里,我该怎么做才能摆脱它?

4

1 回答 1

4

查看最后一个“Caused by:”消息的堆栈跟踪。这是触发整个输出的异常。这是NullPointerException通过调用getString代码的第 21 行生成的:

private String team = getString(R.string.team);

问题是你不能在被要求为你的服务getString()之前打电话。onCreate在此之前,该框架尚未建立将您的服务代码链接到应用程序资源的结构。只需覆盖onCreate并设置team在那里。

. . .
private String team;
. . .

@Override
public void onCreate() {
    team = getString(R.string.team);
}
于 2013-03-29T20:28:43.540 回答