distance.setText((int) bnRoutePlanItem.getLength() / 1000 + "公里");警告Do not concatenate text displayed with `setText`. Use resource string with placeholders. 帮我改一下
时间: 2023-06-19 13:04:34 浏览: 47
可以尝试这样修改:
```
distance.setText(getString(R.string.distance_text, (int) bnRoutePlanItem.getLength() / 1000));
```
在 strings.xml 文件中添加以下内容:
```
<string name="distance_text">%d公里</string>
```
这样可以使用资源字符串和占位符,避免了拼接字符串的警告。
相关问题
view.ratio.setText(e.ratio + "%");这段代码出现Do not concatenate text displayed with `setText`. Use resource string with placeholders. 怎么解决?
### 回答1:
该警告是因为在 `setText()` 方法中直接拼接字符串,这种方式可能会增加内存占用并降低性能。可以使用资源字符串和占位符来解决这个问题,如下所示:
1. 在 `strings.xml` 文件中定义资源字符串,例如:
```xml
<string name="ratio_text">%1$s%%</string>
```
2. 在代码中使用 `getString()` 方法获取资源字符串,然后使用 `String.format()` 方法将占位符替换为实际的值,例如:
```java
String ratioText = getString(R.string.ratio_text, e.ratio);
view.ratio.setText(ratioText);
```
这样可以避免直接拼接字符串,提高代码的可读性和性能。
### 回答2:
问题的原因是代码中使用了`setText`方法对文本内容进行拼接,而不是使用资源字符串和占位符的方式。为了解决这个问题,我们可以将拼接的文本内容放入资源字符串中,并使用占位符来动态替换文本内容。
首先,在`strings.xml`文件(或者相应的资源文件)中定义一个资源字符串,用来存储拼接的文本内容,示例代码如下:
```xml
<string name="view_ratio_text">%s%%</string>
```
接下来,在代码中使用资源字符串和占位符的方式设置文本内容,示例代码如下:
```java
String ratioText = getString(R.string.view_ratio_text, e.ratio);
view.ratio.setText(ratioText);
```
这样,我们就使用了资源字符串和占位符的方式来设置文本内容,解决了代码中出现的警告信息。通过将文本内容放入资源字符串中,可以更好地管理和维护文本内容,同时也提高了代码的可读性和可维护性。
### 回答3:
可以将代码修改为使用字符串资源和占位符来设置文本。具体操作如下:
1. 首先,在strings.xml文件中添加一个字符串资源来存储要显示的文本,例如:
```xml
<string name="ratio_text">%s%%</string>
```
2. 在代码中,使用`getString`方法获取字符串资源,并使用`String.format`方法来设置占位符的值,然后再将其传递给`setText`方法,示例代码如下:
```java
String ratio = String.format(getString(R.string.ratio_text), e.ratio);
view.ratio.setText(ratio);
```
这样修改之后,就不会再出现提示错误" Do not concatenate text displayed with `setText`. Use resource string with placeholders."。同时也能更好地支持多语言和调整格式。
private void initTabView(LinearLayout layoutTab, BNRoutePlanItem bnRoutePlanItem) { TextView prefer = layoutTab.findViewById(R.id.prefer); prefer.setText(bnRoutePlanItem.getPusLabelName()); TextView time = layoutTab.findViewById(R.id.time); time.setText(getString(R.string.route_result_pass_time, (int) bnRoutePlanItem.getPassTime() / 60)); TextView distance = layoutTab.findViewById(R.id.distance); distance.setText(getString(R.string.route_result_distance_text, (int) bnRoutePlanItem.getLength() / 1000)); TextView trafficLight = layoutTab.findViewById(R.id.traffic_light); trafficLight.setText(String.valueOf(bnRoutePlanItem.getLights())); } 报错, java.lang.IllegalStateException: Fragment RouteResultFragment{f466a91} not attached to a context.
这个错误可能是因为您在Fragment中调用了与Context相关的方法,但Fragment未附加到Activity中,导致无法获得正确的Context。您可以尝试在onAttach()方法中保存Activity的Context并在需要时使用它。具体来说,您可以在Fragment中添加以下代码:
```
private Context mContext;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
private void initTabView(LinearLayout layoutTab, BNRoutePlanItem bnRoutePlanItem) {
TextView prefer = layoutTab.findViewById(R.id.prefer);
prefer.setText(bnRoutePlanItem.getPusLabelName());
TextView time = layoutTab.findViewById(R.id.time);
time.setText(getString(R.string.route_result_pass_time, (int) bnRoutePlanItem.getPassTime() / 60));
TextView distance = layoutTab.findViewById(R.id.distance);
distance.setText(getString(R.string.route_result_distance_text, (int) bnRoutePlanItem.getLength() / 1000));
TextView trafficLight = layoutTab.findViewById(R.id.traffic_light);
trafficLight.setText(String.valueOf(bnRoutePlanItem.getLights()));
}
```
这样,您就可以在Fragment中使用Activity的Context了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)