我是android新手,正在编写一个应用程序以基于纬度和经度获取地址。在FetchAddressIntentService类中,在getString()函数中遇到一个错误,指出将字符串添加到string.xml文件后,方法getString(int)未定义。
我遇到的下一个错误是在此行Geocoder geocoder = new Geocoder(this,Locale.getDefault());
说构造函数地址解析器在类中未定义。
这是FetchAddressIntentService类代码:

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;
    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;
    public class FetchAddressIntentService {

        private static final String TAG = null;
        protected ResultReceiver mReceiver;

        protected void onHandleIntent(Intent intent){
           Geocoder geocoder = new Geocoder(this,Locale.getDefault());
           String errorMessage = "";

            // Get the location passed to this service through an extra.
            Location location = intent.getParcelableExtra(
                    Constants.LOCATION_DATA_EXTRA);
            List<Address> addresses = null;

            try {
                addresses = geocoder.getFromLocation(
                        location.getLatitude(),
                        location.getLongitude(),
                        // In this sample, 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>();

                // Fetch the address lines using getAddressLine,
                // join them, and send them to the thread.
                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));
            }

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


有帮助吗?
先感谢您。

最佳答案

您忘记了从FetchAddressIntentService扩展IntentService类。只需添加:

public class FetchAddressIntentService extends IntentService {
...


希望能有所帮助!

关于java - getString()中出现错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35966874/

10-09 07:16