android - 通过反向地理编码检测当前地址时抛出实例化异常

标签 android exception geolocation intentservice reverse-geocoding

我想通过反向地理编码检测地址,因此我创建了一个名为 FetchAddressIntentService 的类,它扩展了 IntentService 并且还完成了 https://developer.android.com/training/location/display-address.html 上的所有步骤。

当调用 startIntentService() 中的 startService(intent) 时,抛出异常(如下)

java.lang.RuntimeException: Unable to instantiate service PACKAGENAME.MainActivity$FetchAddressIntentService: java.lang.InstantiationException: java.lang.Class PACKAGENAME.MainActivity$FetchAddressIntentService has no zero argument constructor

在我的 AndroidManifest.xml 中定义 Intent 服务(如下)

<service
        android:name=".MainActivity$FetchAddressIntentService"
        android:exported="false"/>

</application>

我的 startIntentService 方法(下)

void startIntentService(){
    Intent intent = new Intent(this,FetchAddressIntentService.class);
    intent.putExtra(FetchAddressIntentService.Constants.RECEIVER,myResultReciever);
    intent.putExtra(FetchAddressIntentService.Constants.LOCATION_DATA_EXTRA,lastDetectedLocation);
    startService(intent);
}

我的 FetchAddressIntentService 类(下)

public class FetchAddressIntentService extends IntentService{
        ResultReceiver myReciever;
    public FetchAddressIntentService(){
        super("Fetching address");

    }


    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        String errorMessage = "";
        //Get the location passed to this serviec thorugh an extra
        Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);

        List<Address> address = null;
        try{
            address = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
        }
        catch(Exception e){
            Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show();
        }

        //Handle the case when there is no location found
        if(address == null || address.size() == 0){
            Toast.makeText(this, "No address found", Toast.LENGTH_LONG).show();
            deliverResulttoReciever(Constants.FAILURE_RESULT,"No address Found");
        }
        else{
            Address currentAddress = address.get(0);
            ArrayList<String> addressFragment = new ArrayList<String>();

            //Fetch the address lines using getAddressLine
            //join them and send them to the thread
            for(int i = 0;i<=currentAddress.getMaxAddressLineIndex();i++)
            {
                addressFragment.add(currentAddress.getAddressLine(i));
            }
            deliverResulttoReciever(Constants.SUCCESS_RESULT, TextUtils.join(System.getProperty("line.saparator"),addressFragment));
        }


    }

    private void deliverResulttoReciever(int resultCode, String message) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.RESULT_DATA_KEY,message);
        myReciever.send(resultCode,bundle);
    }

    public final class Constants {
        public static final int SUCCESS_RESULT = 0;
        public static final int FAILURE_RESULT = 1;
        public static final String PACKAGE_NAME =
                "com.google.android.gms.location.sample.locationaddress";
        public static final String RECEIVER = PACKAGE_NAME + ".RECEIVER";
        public static final String RESULT_DATA_KEY = PACKAGE_NAME +
                ".RESULT_DATA_KEY";
        public static final String LOCATION_DATA_EXTRA = PACKAGE_NAME +
                ".LOCATION_DATA_EXTRA";
    }
}

最佳答案

试试这个:

将您的服务类更改为静态:

 public static class FetchAddressIntentService extends IntentService {

        public FetchAddressIntentService(){
            super("Fetching address");

           }

有关更多信息,请阅读 this

关于android - 通过反向地理编码检测当前地址时抛出实例化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447312/

相关文章:

android - Android中屏幕闪烁的最快方法

android - 将默认区域设置设置为 DatePicker 和 TimePicker

c# - 你调用的对象是空的。在 C# 中将数据表绑定(bind)到 datagridview 时

javascript - JMeter 找不到 BSF Sampler 的方法

javascript - Phonegap 2.5.0 地理定位不适用于 android 4.0.4

android - 来自 GPS、网络和 Wifi 的 Cordova/Phonegap 地理定位

android - 以编程方式检索 Android 库的版本信息

java - 为什么会出现握手失败 (Java SSL)

elasticsearch - 如何使用Logstash中要在Kibana中使用的JDBC输入插件将纬度和经度值映射到转换数据库的geo_point中?

安卓 NDK : WARNING: APP_PLATFORM android-9 is larger than android:minSdkVersion 8