2016-09-20 60 views
-1
E/FetchAddressIS: No receiver received. There is nowhere to send the results. 

我試圖使用intent服務從lastKnownLocation對象獲得街道名稱。這是投擲hissy,我看不出爲什麼。意圖服務「沒有收到接收器」當獲取地址

該代碼來自Google開發人員,這正是我所需要的,除了不工作的位。

我試過(1)在常量中更改包名,以及我需要做的確定!但仍然是錯誤。 我已經瀏覽了我的MainActivity,看看我是否改變了LastKnownLocation,或者在調用之前不分配它,但它總是有一個有效的值。

所以我現在處於虧損狀態,會喜歡一些專家的眼睛和學習。即插即用,而不是插電和祈禱。

package com.example.android.recyclingbanks; 

import android.app.IntentService; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.os.Bundle; 
import android.os.ResultReceiver; 
import android.text.TextUtils; 
import android.util.Log; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

/** 
* Asynchronously handles an intent using a worker thread. Receives a ResultReceiver object and a 
* location through an intent. Tries to fetch the address for the location using a Geocoder, and 
* sends the result to the ResultReceiver. 
*/ 
public class FetchAddressIntentService extends IntentService { 
    private static final String TAG = "FetchAddressIS"; 

    /** 
    * The receiver where results are forwarded from this service. 
    */ 
    protected ResultReceiver mReceiver; 

    /** 
    * This constructor is required, and calls the super IntentService(String) 
    * constructor with the name for a worker thread. 
    */ 
    public FetchAddressIntentService() { 
     // Use the TAG to name the worker thread. 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.wtf("onHandleIntent", "error already?!"); 
     String errorMessage = ""; 

     mReceiver = intent.getParcelableExtra(Constants.RECEIVER); 

     // Check if receiver was properly registered. 
     if (mReceiver == null) { 
      Log.wtf(TAG, "No receiver received. There is nowhere to send the results."); 
      return; 
     } 

     // Get the location passed to this service through an extra. 
     Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA); 

     // Make sure that the location data was really sent over through an extra. If it wasn't, 
     // send an error error message and return. 
     if (location == null) { 
      errorMessage = getString(R.string.no_location_data_provided); 
      Log.wtf(TAG, errorMessage); 
      deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage); 
      return; 
     } 

     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 

     // Address found using the Geocoder. 
     List<Address> addresses = null; 

     try { 
      addresses = geocoder.getFromLocation(
        location.getLatitude(), 
        location.getLongitude(), 
        // In this sample, we get just a single address. 
        1); 
     } catch (IOException ioException) { 
      // Catch network or other I/O problems. 
      errorMessage = getString(R.string.service_not_available); 
      Log.e(TAG, errorMessage, ioException); 
     } catch (IllegalArgumentException illegalArgumentException) { 
      // Catch invalid latitude or longitude values. 
      errorMessage = getString(R.string.invalid_lat_long_used); 
      Log.e(TAG, errorMessage + ". " + 
        "Latitude = " + location.getLatitude() + 
        ", Longitude = " + location.getLongitude(), illegalArgumentException); 
     } 

     // Handle case where no address was found. 
     if (addresses == null || addresses.size() == 0) { 
      if (errorMessage.isEmpty()) { 
       errorMessage = getString(R.string.no_address_found); 
       Log.e(TAG, errorMessage); 
      } 
      deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage); 
     } else { 
      Address address = addresses.get(0); 
      ArrayList<String> addressFragments = new ArrayList<String>(); 


      for(int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
       addressFragments.add(address.getAddressLine(i)); 
      } 
      Log.i(TAG, getString(R.string.address_found)); 
      deliverResultToReceiver(Constants.SUCCESS_RESULT, 
        TextUtils.join(System.getProperty("line.separator"), addressFragments)); 
     } 
    } 

    /** 
    * Sends a resultCode and message to the receiver. 
    */ 
    private void deliverResultToReceiver(int resultCode, String message) { 
     Bundle bundle = new Bundle(); 
     bundle.putString(Constants.RESULT_DATA_KEY, message); 
     mReceiver.send(resultCode, bundle); 
    } 
} 

請幫幫我!這是我的應用程序最後一個交易斷路器問題,來自GPS的地理編碼對用戶來說是一個不錯的小觸摸。

+1

是如何意圖在MainActivity開始創建,調用'onHandleIntent'在'IntentService'? –

+1

您的ResultReciever中是否有可供分析的CREATOR?你是將它創建爲一個匿名或非靜態的內部類嗎? – FunkTheMonk

+1

'Intent'你發送到'FetchAddressIntentService'沒有'Constants.RECEIVER'多餘的應該是一個'ResultReciever'對象實例 – pskink

回答

0

感謝您的關注傢伙,你有很大的啓示,我回頭看着它,問題是用MainActivity,我沒有intialised

mResultReceiver =新AddressResultReceiver(新的處理程序());

看起來我需要搜索更長一點,所以重複!

ResultReceiver gives Nullpointer exception