2016-09-20 63 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