位置和计时器以及nullpointerexception

时间:2012-03-03 02:07:33

标签: android location nullpointerexception

我建立了一项服务,使用GPS和wifi计算用户的位置(由我自己编写)。计算完成后,将SMS发送回用户。请注意,此服务由SMS启动。无线和GPS与自己的工作正常。但是在组合在一起时会出错。

实际上我遇到了两个错误,首先是:java.lang.NullPointerException。

然后我的应用程序再次崩溃,我得到了这个:java.lang.RuntimeException:无法使用null启动服务your.com.HelloWifi.ScanService@405253e0:java.lang.NullPointerException

似乎GPS正在尝试在第一次服务完成后启动另一项服务。我尝试了很多方法,但无法解决这个问题。有人伸出援助之手。

以下是代码

public class ScanService extends Service {
    WifiManager wifi;
    TimerTask timerTask1;
    private static Timer timer = new Timer();
    String receipt;
    String message = "";
    int count = 0;
    Location location;
    String gpsInfo = "";
    LocationManager locationManager;

@Override
public void onCreate() {
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    timer = new Timer();
    timerTask1 = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub

            Log.d("timer", "timer alright");
            if (count >=0 && count <= 24) {
                count++;
                scan();//do the scanning
            }
            else if (count == 25) {
                locating();//do the calculation
                sendSMS();

            }
        }
    };
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(provider);
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        message += " http://ditu.google.cn/?q=" + lat + "," + lng;
    }
    else {
        message += "unable to get gps info";
    }
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Toast.makeText(this, "service onStart", 0).show();
    wifi.setWifiEnabled(false);
    wifi.setWifiEnabled(true);
    timer.scheduleAtFixedRate(timerTask1, 30000, 500);

}

@Override
public void onDestroy() {
    super.onDestroy();
    timer.cancel();
    Toast.makeText(this, "service onDestroy", Toast.LENGTH_LONG).show();
}
public void sendSMS() {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
    Log.d("sendSMS", "in sndSMS");
    //Log.d("message", message);

    BroadcastReceiver sentReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch(getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent", 
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure", 
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service", 
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", 
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off", 
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }

    };

    BroadcastReceiver deliveredReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch(getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS delivered", 
                        Toast.LENGTH_SHORT).show();
                stopSelf();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered", 
                        Toast.LENGTH_SHORT).show();
                stopSelf();
                break; 
            }

        }

    };
    registerReceiver(sentReceiver, new IntentFilter(SENT));
    registerReceiver(deliveredReceiver, new IntentFilter(DELIVERED));
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(receipt, null, message, sentPI, deliveredPI);

    timer.cancel();
    Log.d("timer", "timer cancelled");
}

}

1 个答案:

答案 0 :(得分:0)

终于搞清楚了!短信太长了!添加以下代码后,问题解决了!

    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, ScanService.class), 0);                
    SmsManager sms = SmsManager.getDefault();
    List<String> texts = sms.divideMessage(message);
    for (String text:texts) {

        sms.sendTextMessage(receipt, null, text, pi, null);
    }