我正在1.6版本中開發基於LBS的應用程序,它運行得非常好。此前它的運行良好,更新版本如2.2 & 2.3.3。但是現在突然間,它開始在我用於後臺進程的AsyncTask類中給出空指針異常。這是我的錯誤日誌的代碼片段。AsyncTask總是拋出空指針異常
public class AndroidLocationActivity extends Activity {
public String provider;
public double latitude, longitude = 0;
public LocationListener locationListener = new MyLocationListener();
public LocationManager locationManager;
public CurrentPositionTask getCurrentLocation;
public long time=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
setCriteria();
runAsyncTask();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
time = 0;
locationManager.removeUpdates(locationListener);
}
public void setCriteria() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
provider = locationManager.getBestProvider(criteria, true);
//Toast.makeText(getApplicationContext(), "Provider - " + provider, Toast.LENGTH_SHORT).show();
if (provider == null) {
provider = LocationManager.GPS_PROVIDER;
}
}
public void runAsyncTask() {
// TODO Auto-generated method stub
if (getCurrentLocation == null) {
getCurrentLocation = new CurrentPositionTask(AndroidLocationActivity.this);
}
if (getCurrentLocation != null) {
getCurrentLocation.execute("Searching for Location");
}
}
public boolean checkConnection()
{
//ARE WE CONNECTED TO THE NET
ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
class CurrentPositionTask extends AsyncTask<String, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(AndroidLocationActivity.this);
Location location = new Location(locationManager.getLastKnownLocation(provider));
Context context;
public CurrentPositionTask(Context mContext) {
this.context = mContext;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
time = location.getTime();
Toast.makeText(AndroidLocationActivity.this, String.valueOf(time), Toast.LENGTH_LONG).show();
if (checkConnection()) {
//Toast.makeText(getApplicationContext(), "Internet", Toast.LENGTH_LONG).show();
Dialog.setTitle("Loading");
Dialog.setMessage("Searching for Location");
Dialog.show();
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
}
else {
Toast.makeText(getApplicationContext(), "Internet is Not Available", Toast.LENGTH_LONG).show();
}
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
if (time >= 100000) {
latitude = 0;
longitude = 0;
}
while (latitude == 0 && longitude == 0) {
/*latitude = (float) (location.getLatitude());
longitude = (float) (location.getLongitude());*/
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(AndroidLocationActivity.this, "Location Floats:- " + latitude + "," + longitude, Toast.LENGTH_LONG).show();
super.onPostExecute(result);
if (Dialog != null && Dialog.isShowing()) {
Dialog.dismiss();
time=0;
Intent homeIntent = new Intent(AndroidLocationActivity.this.getApplicationContext(), HomeMenuActivity.class);
homeIntent.putExtra("lat", latitude);
homeIntent.putExtra("lng", longitude);
startActivity(homeIntent);
}
locationManager.removeUpdates(locationListener);
}
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
錯誤日誌: -
12-04 16:59:04.848: E/AndroidRuntime(4165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidlocation/com.androidlocation.AndroidLocationActivity}: java.lang.NullPointerException
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.os.Looper.loop(Looper.java:123)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.ActivityThread.main(ActivityThread.java:3701)
12-04 16:59:04.848: E/AndroidRuntime(4165): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 16:59:04.848: E/AndroidRuntime(4165): at java.lang.reflect.Method.invoke(Method.java:507)
12-04 16:59:04.848: E/AndroidRuntime(4165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
12-04 16:59:04.848: E/AndroidRuntime(4165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
12-04 16:59:04.848: E/AndroidRuntime(4165): at dalvik.system.NativeStart.main(Native Method)
12-04 16:59:04.848: E/AndroidRuntime(4165): Caused by: java.lang.NullPointerException
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.location.Location.set(Location.java:119)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.location.Location.<init>(Location.java:112)
**12-04 16:59:04.848: E/AndroidRuntime(4165): at com.androidlocation.AndroidLocationActivity$CurrentPositionTask.<init>(AndroidLocationActivity.java:84)**
12-04 16:59:04.848: E/AndroidRuntime(4165): at com.androidlocation.AndroidLocationActivity.runAsyncTask(AndroidLocationActivity.java:60)
12-04 16:59:04.848: E/AndroidRuntime(4165): at com.androidlocation.AndroidLocationActivity.onCreate(AndroidLocationActivity.java:31)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 16:59:04.848: E/AndroidRuntime(4165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
3error這是我從日誌,1調用runAsyncTask()方法,2創造異步的對象的時間得到任務類和第三個在初始化asynctask類中的位置變量時。
如果有人有任何想法請幫我解決它。謝謝...
doInBackGround是應該做的繁重任務。據我所見,大部分繁重的任務都在preExecute中。 doinBackGround在工作線程中運行。它是否應該直接更新在UI線程上定義的成員? – user994886
不,我不直接更新UI線程....而且我在preExecute中調用requestUpdates方法,就好像我從doInBackground調用它,它給了我錯誤。 – Scorpion
任何人有任何關於這個錯誤的想法?如果有人知道這件事,請請幫助我。謝謝 – Scorpion