gpt4 book ai didi

java - getString() 中出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:32 26 4
gpt4 key购买 nike

我是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);
}
}

有什么帮助吗?预先感谢您。

最佳答案

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

public class FetchAddressIntentService extends IntentService {
...

希望有帮助!!

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com