我正在尝试在AsyncTask中获取用户当前位置,并将Location对象作为返回值传回给调用活动(其中包含Latitude,Longitude等)。
但每当我从我的活动中调用Asynctask时就像这样
new AsyncTask_GetLocation(Navigation_Drawer.this,location_interface,1,1,LocationRequest.PRIORITY_HIGH_ACCURACY).execute();
以上行将启动AsyncTask以获取位置。
public class AsyncTask_GetLocation extends AsyncTask<Void, Void, Location> implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
Context context;
GoogleApiClient LocationapiClient;
LocationRequest mLocationRequest;
Location mCurrentLocation;
LocationManager locationManager;
Location_Interface locationInterface;
int Interval;
int Fastest;
int Priority;
public static final String TAG="###GET LOCATION###";
public AsyncTask_GetLocation(Context context,Location_Interface location_interface, int interval, int fastest, int priority) {
this.context = context;
Interval = interval;
Fastest = fastest;
Priority = priority;
locationInterface=location_interface;
}
@Override
protected Location doInBackground(Void... params) {
Log.d(TAG,"Do in Background Running "+context);
GoogleAPI(context);
LocationapiClient.connect();
if (mCurrentLocation!=null){
Log.d(TAG,"Current Location Is Not Null "+mCurrentLocation);
return mCurrentLocation;
}
return null;
}
@Override
protected void onPostExecute(Location location) {
super.onPostExecute(location);
locationInterface.userCurrent_Location(location);
LocationServices.FusedLocationApi.removeLocationUpdates(LocationapiClient,this);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG, "ON CONNECTED CALLED GOOGLE API CONNECTED ");
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(LocationapiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG,"On Location Changed");
mCurrentLocation=location;
}
private void requestLocationUpdate(int Interval,int Fastest,int Priority) {
Log.d(TAG, "Request Location Update Calling");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(Interval);
mLocationRequest.setFastestInterval(Fastest);
mLocationRequest.setPriority(Priority);
}
public synchronized void GoogleAPI(Context apiContext){
requestLocationUpdate(Interval,Fastest,Priority);
Log.d(TAG,"Location Api Client Value "+LocationapiClient);
if (LocationapiClient==null){
LocationapiClient = new GoogleApiClient.Builder(apiContext) // THIS LINE THROW NPE
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
LocationapiClient.connect();
}
}
logcat的
04-26 18:33:18.325 15252-15725/com.example.com.Loc D/###GET LOCATION###: Location Api Client Value null
04-26 18:33:18.325 15252-15725/com.example.com.Loc W/dalvikvm: threadid=21: thread exiting with uncaught exception (group=0x40d53378)
04-26 18:33:18.425 15252-15725/com.example.com.Loc E/UncaughtException: 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 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
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 android.content.ContextWrapper.getMainLooper(ContextWrapper.java:96)
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at com.example.com.Loc.Location_system.AsyncTask_GetLocation.GoogleAPI(AsyncTask_GetLocation.java:102)
at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:48)
at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:24)
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 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
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)
04-26 18:33:18.695 15252-15334/com.example.com.Loc I/FA: Tag Manager is not found and thus will not be used
04-26 18:33:18.785 15252-15334/com.example.com.Loc D/FA: Logging event (FE): _ae, Bundle[{_o=crash, timestamp=1493211798437, fatal=1}]
04-26 18:33:18.935 15252-15334/com.example.com.Loc D/FA: Connected to remote service
04-26 18:33:19.335 15252-15725/com.example.com.Loc E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
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 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
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 android.content.ContextWrapper.getMainLooper(ContextWrapper.java:96)
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at com.example.com.Loc.Location_system.AsyncTask_GetLocation.GoogleAPI(AsyncTask_GetLocation.java:102)
at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:48)
at com.example.com.Loc.Location_system.AsyncTask_GetLocation.doInBackground(AsyncTask_GetLocation.java:24)
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 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
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)
04-26 18:33:19.525 15252-15252/com.example.com.Loc D/###Navigation Drawer###: ON PAUSE CALLING
答案 0 :(得分:0)
你可以在`logcat&#39;中看到该方法试图调用:
getMainLooper()
这是一个访问主线程的调用,因为你在AsyncTask
的后台线程中调用它,它就会抛出错误。
我建议你将GoogleApiClient
的初始化调用移到主线程,方法是将代码放在Activity
的生命周期中,或者使用onPreExecute
AsyncTask
为这类事情提供的方法,当你需要在开始你想要在后台完成的工作之前访问主线程时:
@Override
protected void onPreExecute() {
GoogleAPI(context);
}