0

我正在嘗試在AsyncTask中獲取用戶當前位置,並將位置對象作爲返回值傳遞給調用活動(其中包含經度,緯度等)。GoogleAPIclient nullpointerexeption

但每當我打電話的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

您可在'logcat的」,該方法試圖調用看到:

getMainLooper() 

這是一個呼籲以訪問主線程,因爲你在AsyncTask的後臺線程中調用它,它會拋出erroe。

我建議你就進入初始化呼籲GoogleApiClient主線程,無論是放在Activity的生命週期中的代碼,或通過使用onPreExecute方法AsyncTask供應你只是這樣的事情當你需要訪問主線程開始之前要在後臺做的工作:

@Override 
    protected void onPreExecute() { 
     GoogleAPI(context); 
    } 
+0

我想你的方式,但它給了我同樣的error.Even我執行'GoogleAPI前檢查上下文值(上下文);'不是null。我將我的'GoogleAPI(context);'移動到'onPreExecute',這給了我相同的錯誤,並且在日誌中我還可以看到'at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:96)' – Ritu

+0

什麼是'Navigation_Drawer。 this'? – yshahak

+0

我幾乎可以肯定,你交給'AsyncTask'的'Context'不好,它需要是'Activity'上下文。我在我的項目中運行你的代碼,只做了很小的修改,並且工作。 – yshahak