如何解决解决 android.content.res.Resources 错误的问题
我了解此错误已多次解决,我已检查了其中的大部分,例如 Android: android.content.res.Resources$NotFoundException: String resource ID #0x5、android.content.res.Resources$NotFoundException: String resource ID 以查看是否可以找到解决问题的方法,但我不能,而且大多数其中很难理解。我不知道我的代码是不是整数/字符串,所以我不知道到底要纠正什么,所以我需要帮助。
我在运行时收到此错误:
android.content.res.Resources$NotFoundException: String resource ID #0x601ee924
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.TextView.setText(TextView.java:5848)
at com.tex.lightweatherforecast.Activity.HomeActivity$1.onResponse(HomeActivity.java:66)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$3wC8FyV4pyjrzrYL5U0mlYiviZw.run(UnkNown Source:6)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
我的代码:
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
public static String BaseUrl = "https://api.openweathermap.org";
public static String AppId = "";
public static String lat = "9.0574";
public static String lon = "7.4898";
// User Timezone name,current time
TextView time_zone,time_field;
ConstraintLayout constraintLayout;
public static int count=0;
int[] drawable =new int[]{R.drawable.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
Timer _t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
time_zone = findViewById(R.id.textView4);
time_field = findViewById(R.id.textView9);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this,R.id.fragment);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
Retrofit retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
WeatherService service = retrofit.create(WeatherService.class);
Call<WeatherResponse> call = service.getCurrentWeatherData(lat,lon,AppId);
call.enqueue(new Callback<WeatherResponse>() {
@Override
public void onResponse(@NonNull Call<WeatherResponse> call,@NonNull Response<WeatherResponse> response) {
if (response.code() == 200) {
WeatherResponse weatherResponse = response.body();
assert weatherResponse != null;
assert response.body() != null;
time_zone.setText(response.body().getTimezone());
time_field.setText(response.body().getCurrent().getDt());
constraintLayout = findViewById(R.id.layout);
constraintLayout.setBackgroundResource(R.drawable.dubai);
_t = new Timer();
_t.scheduleAtFixedrate(new TimerTask() {
@Override
public void run() {
// run on ui thread
runOnUiThread(() -> {
if (count < drawable.length) {
constraintLayout.setBackgroundResource(drawable[count]);
count = (count + 1) % drawable.length;
}
});
}
},5000,5000);
}
}
@Override
public void onFailure(@NotNull Call<WeatherResponse> call,@NotNull Throwable t) {
}
});
}
}
FirstFragment.java
public class FirstFragment extends Fragment {
public static String BaseUrl = "https://api.openweathermap.org";
public static String AppId = "";
public static String lat = "9.0574";
public static String lon = "7.4898";
// User Timezone name,current time,current temperature,current condition,sunrise,sunset,temperature,pressure,humidity,wind_speed,visibility,UV Index
TextView current_temp,current_output,rise_time,set_time,temp_out,Press_out,Humid_out,Ws_out,Visi_out,UV_out;
// Todo: Rename parameter arguments,choose names that match
// the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
private static final String ARG_ParaM1 = "param1";
private static final String ARG_ParaM2 = "param2";
// Todo: Rename and change types of parameters
private String mParam1;
private String mParam2;
public FirstFragment() {
// required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SecondFragment.
*/
// Todo: Rename and change types and number of parameters
public static FirstFragment newInstance(String param1,String param2) {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putString(ARG_ParaM1,param1);
args.putString(ARG_ParaM2,param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_ParaM1);
mParam2 = getArguments().getString(ARG_ParaM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first,container,false);
current_temp = rootView.findViewById(R.id.textView10);
current_output = rootView.findViewById(R.id.textView11);
rise_time = rootView.findViewById(R.id.textView25);
set_time = rootView.findViewById(R.id.textView26);
temp_out = rootView.findViewById(R.id.textView28);
Press_out = rootView.findViewById(R.id.textView29);
Humid_out = rootView.findViewById(R.id.textView30);
Ws_out = rootView.findViewById(R.id.textView33);
Visi_out = rootView.findViewById(R.id.textView34);
UV_out = rootView.findViewById(R.id.textView35);
Retrofit retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
WeatherService service = retrofit.create(WeatherService.class);
Call<WeatherResponse> call = service.getCurrentWeatherData(lat,AppId);
call.enqueue(new Callback<WeatherResponse>() {
@Override
public void onResponse(@NonNull Call<WeatherResponse> call,@NonNull Response<WeatherResponse> response) {
if (response.code() == 200) {
WeatherResponse weatherResponse = response.body();
assert weatherResponse != null;
assert response.body() != null;
current_temp.setText(getString(R.string.blank,response.body().getCurrent().getTemp() + " ℃"));
current_output.setText(response.body().getCurrent().getWeather().get(0).getDescription());
rise_time.setText(getString(R.string.blank,response.body().getCurrent().getSunrise() + " AM"));
set_time.setText(getString(R.string.blank,response.body().getCurrent().getSunset() + " PM"));
temp_out.setText(getString(R.string.blank,response.body().getCurrent().getTemp() + " ℃"));
Press_out.setText(getString(R.string.blank,response.body().getCurrent().getPressure() + " hpa"));
Humid_out.setText(getString(R.string.blank,response.body().getCurrent().getHumidity() + " %"));
Ws_out.setText(getString(R.string.blank,response.body().getCurrent().getwindSpeed() + " Km/h"));
Visi_out.setText(getString(R.string.blank,response.body().getCurrent().getVisibility() + " m"));
}
}
@Override
public void onFailure(@NonNull Call<WeatherResponse> call,@NonNull Throwable t) {
t.printstacktrace();
}
});
return rootView;
}
}
在我的 String.xml 中:
<string name="blank">%s ℃</string>
解决方法
android.content.res.Resources$NotFoundException: String resource ID #0x601ee924
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.TextView.setText(TextView.java:5848)
at com.tex.lightweatherforecast.Activity.HomeActivity$1.onResponse(HomeActivity.java:66)
如上所示,异常在 HomeActivity
方法行 (66) 的 onResponse()
中引发,您尝试将文本设置为 TextView
。
因此,在 HomeActivity
中,您:
time_field.setText(response.body().getCurrent().getDt());
并且根据 documentation,.getDt()
返回一个整数,因此您不能将其直接设置为 TextView
,除非您将其转换为字符串。
要解决此问题,请用以下代码之一替换该行代码:
// 1st
time_field.setText(String.valueOf(response.body().getCurrent().getDt()));
// 2nd
time_field.setText("" + response.body().getCurrent().getDt());
,
您需要将 position
的值转换为 String
。与另一个 String
连接是一种方法
setText("" + position)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。