【发布时间】:2014-02-27 03:03:06
【问题描述】:
我有一个测试屏幕,其中有一个按钮 - 按下它会调用服务。我正在尝试实现一种方法来获取当前用户的当前 gps 位置,并且在尝试调用时崩溃。谁能告诉我是什么问题?
package com.example.whereyouapp;
import java.util.Arrays;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.widget.Toast;
public class Controller extends Service{
private static final int POLL_INTERVAL = 1000 *3;
static int number_of_times=0;
Location location;
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
public void makeUseOfNewLocation(Location location){
this.location=location;
}
public static double coordinatesDistance(double lat1, double lon1, double lat2, double lon2){//returns distance in kilometers between two coordiantes
double deltaLat = Math.toRadians(lat2-lat1);
double deltaLong = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) + Math.sin(deltaLong / 2) * Math.sin(deltaLong / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return 6371 * c;
}
public int onStartCommand(Intent intent,int flags, int startId){
Toast.makeText(this, "yololo- the service class works", Toast.LENGTH_SHORT).show();
//sendSMS("5613500110","If you received this text message then the Service class for WhereYouApp works");
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, locationListener);
if(location!=null){
double distance=coordinatesDistance(location.getLatitude(),location.getLatitude(),location.getLatitude()+2,location.getLatitude()+2);
Toast.makeText(this, ""+distance, Toast.LENGTH_SHORT).show();
}
if(number_of_times==5){
setServiceAlarm(getBaseContext(),false);
number_of_times=-1;
}
number_of_times++;
return START_STICKY;
}
public void onDestroy(){
super.onDestroy();
Toast.makeText(this, "yolo- the service has stopped working", Toast.LENGTH_LONG).show();
}
public void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
public static void setServiceAlarm(Context context, boolean isOn){
Intent i = new Intent(context, Controller.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (isOn) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), POLL_INTERVAL, pi);
} else {
alarmManager.cancel(pi);
pi.cancel();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
这个类将用于测试控制器功能,它将由一个带有按钮的屏幕组成,这些按钮可以执行各种功能
package com.example.whereyouapp;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.view.View;
import android.widget.Button;
public class ControllerTestingScreen extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controller_test_screen);
//Set button colors to red, green, blue, and red, respectively
Button button = (Button) findViewById(R.id.testbutton);
button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
OnClickListener buttonListener = new View.OnClickListener(){
public void onClick(View arg0) {
Controller.setServiceAlarm(getBaseContext(), true);
}
};
button.setOnClickListener(buttonListener);
}
}
Logcat:
02-26 22:04:38.317: D/AndroidRuntime(1427): Shutting down VM
02-26 22:04:38.317: W/dalvikvm(1427): threadid=1: thread exiting with uncaught exception (group=0xb1a91ba8)
02-26 22:04:38.457: D/dalvikvm(1427): GC_FOR_ALLOC freed 115K, 6% free 3270K/3460K, paused 87ms, total 103ms
02-26 22:04:38.497: E/AndroidRuntime(1427): FATAL EXCEPTION: main
02-26 22:04:38.497: E/AndroidRuntime(1427): Process: com.example.whereyouapp, PID: 1427
02-26 22:04:38.497: E/AndroidRuntime(1427): java.lang.RuntimeException: Unable to instantiate service com.example.whereyouapp.Controller: java.lang.NullPointerException
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556)
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.app.ActivityThread.access$1800(ActivityThread.java:135)
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.os.Looper.loop(Looper.java:136)
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-26 22:04:38.497: E/AndroidRuntime(1427): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 22:04:38.497: E/AndroidRuntime(1427): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 22:04:38.497: E/AndroidRuntime(1427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-26 22:04:38.497: E/AndroidRuntime(1427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-26 22:04:38.497: E/AndroidRuntime(1427): at dalvik.system.NativeStart.main(Native Method)
02-26 22:04:38.497: E/AndroidRuntime(1427): Caused by: java.lang.NullPointerException
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:540)
02-26 22:04:38.497: E/AndroidRuntime(1427): at com.example.whereyouapp.Controller.<init>(Controller.java:30)
02-26 22:04:38.497: E/AndroidRuntime(1427): at java.lang.Class.newInstanceImpl(Native Method)
02-26 22:04:38.497: E/AndroidRuntime(1427): at java.lang.Class.newInstance(Class.java:1208)
02-26 22:04:38.497: E/AndroidRuntime(1427): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
02-26 22:04:38.497: E/AndroidRuntime(1427): ... 10 more
02-26 22:04:45.187: I/Process(1427): Sending signal. PID: 1427 SIG: 9
02-26 22:04:47.507: D/AndroidRuntime(1469): Shutting down VM
02-26 22:04:47.507: W/dalvikvm(1469): threadid=1: thread exiting with uncaught exception (group=0xb1a91ba8)
02-26 22:04:47.517: E/AndroidRuntime(1469): FATAL EXCEPTION: main
02-26 22:04:47.517: E/AndroidRuntime(1469): Process: com.example.whereyouapp, PID: 1469
02-26 22:04:47.517: E/AndroidRuntime(1469): java.lang.RuntimeException: Unable to instantiate service com.example.whereyouapp.Controller: java.lang.NullPointerException
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556)
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.app.ActivityThread.access$1800(ActivityThread.java:135)
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.os.Looper.loop(Looper.java:136)
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-26 22:04:47.517: E/AndroidRuntime(1469): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 22:04:47.517: E/AndroidRuntime(1469): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 22:04:47.517: E/AndroidRuntime(1469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-26 22:04:47.517: E/AndroidRuntime(1469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-26 22:04:47.517: E/AndroidRuntime(1469): at dalvik.system.NativeStart.main(Native Method)
02-26 22:04:47.517: E/AndroidRuntime(1469): Caused by: java.lang.NullPointerException
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:540)
02-26 22:04:47.517: E/AndroidRuntime(1469): at com.example.whereyouapp.Controller.<init>(Controller.java:30)
02-26 22:04:47.517: E/AndroidRuntime(1469): at java.lang.Class.newInstanceImpl(Native Method)
02-26 22:04:47.517: E/AndroidRuntime(1469): at java.lang.Class.newInstance(Class.java:1208)
02-26 22:04:47.517: E/AndroidRuntime(1469): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
02-26 22:04:47.517: E/AndroidRuntime(1469): ... 10 more
【问题讨论】:
-
您是否在 Manifest 文件中授予权限?
-
logcat 在 com.example.whereyouapp.Controller.
(Controller.java:30) 非常清楚,问题是什么,为什么是问题?怎么会有人知道第 30 行在哪里? -
哇……好强悍的人群! Sid 可能在您的待定时进行了编辑,Ked(只是我的猜测)。
-
被社区拒绝了♦ 所以,是的可能。
-
让我们都退后一步。这只是一个编辑......不值得争论。 @Kedarnath,您可以通过单击个人资料中的“建议”选项卡并单击编辑来查看谁批准/拒绝了您的编辑。
标签: android gps location alarmmanager