问题描述
我正在运行一个AlarmManager
,它使用此方法每x Seconds
触发一次Serivce
:
Intent intent = new Intent(AndroidGPSTrackingActivity.this,
AppService.class);
PendingIntent sender =
PendingIntent.getService(AndroidGPSTrackingActivity.this,
0, intent, 0);
AlarmManager am = (AlarmManager)
getSystemService(ALARM_SERVICE);
am.cancel(sender);
服务类在清单中声明为: <service android:name=".AppService" />
。
AppService.java:
public class AppService extends Service {
GPSTracker gps;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Service Created", 1).show();
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Service Running ", 1).show();
PowerManager pm = (PowerManager) this.getSystemService(this.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
gps = new GPSTracker(this);
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
SendLocation SendLocation = new SendLocation();
SendLocation.execute(latitude, longitude);
} else {
gps.showSettingsAlert();
}
wl.release();
return super.onStartCommand(intent, flags, startId);
}
}
SendLocation.java:
public class SendLocation extends AsyncTask<Object, Void, Void> {//line 18
private Double latitude, longitude;
private String userName, password;
GlobalClass Global = new GlobalClass();
protected Void doInBackground(Object... params) {
latitude = (Double) params[0];
longitude = (Double) params[1];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Global.getHostServer());//line 28
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("strLat", Double
.toString(latitude)));
nameValuePairs.add(new BasicNameValuePair("strLong", Double
.toString(longitude)));
nameValuePairs.add(new BasicNameValuePair("userName", Global.getUserName()));
nameValuePairs.add(new BasicNameValuePair("password", Global.getPassword()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseStr = EntityUtils.toString(response.getEntity());
Log.d("Response",responseStr);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
}
它工作了一段时间,然后突然出现两个错误,一个在18行,一个在28行,然后最终...强制关闭。 我为何要关闭这支部队有任何想法吗?
更新:当试图释放内存时android自动杀死应用程序时,结果证明是强制关闭。 如何解决此问题?
LogCat:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at java.net.URI.parseURI(URI.java:353)
at java.net.URI.<init>(URI.java:204)
at java.net.URI.create(URI.java:725)
at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:28)
at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
03-10 00:43:32.803 1693-1693/com.example.gpstracking E/Trace﹕ error opening trace file: No such file or directory (2)
03-10 00:43:32.979 1693-1708/com.example.gpstracking E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at java.net.URI.parseURI(URI.java:353)
at java.net.URI.<init>(URI.java:204)
at java.net.URI.create(URI.java:725)
at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:28)
at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
1楼
我在您的代码中看到的唯一与您AsyncTask的Params
参数有关的怪异事物。
您的声明如下所示:
public class SendLocation extends AsyncTask<Object, Void, Void>
其中Params
被定义为Object
,但是在调用execute
时,您正在传递的latitude
和longitude
是double
类型的变量。
SendLocation.execute(latitude, longitude);
您应该这样更改AsyncTask的定义:
public class SendLocation extends AsyncTask<Double, Void, Void>
2楼
事实证明,在App Force Close上,它将关闭我的Global Class,而我将没有Global变量。 我通过将所有信息作为参数传递来修复它,如下所示:
protected Void doInBackground(Object... params) {
latitude = (Double) params[0];
longitude = (Double) params[1];
userName = (String) params[2];
password = (String) params[3];
hostServer = (String) params[4];
并在服务中调用它,例如:
SendLocation.execute(latitude, longitude, Global.getUserName(), Global.getPassword(), Global.getHostServer());