textView19 = findViewById(R.id.textView19);
时间: 2024-05-17 19:18:03 浏览: 10
这是一行代码,用于在 Android 应用程序中找到布局文件中的 TextView 控件,并将其与代码中的 TextView 对象进行关联。其中,R.id.textView19 是 TextView 控件在布局文件中的唯一标识符。findViewById() 方法会返回一个 View 对象,因此需要将其转换为 TextView 对象,以便可以在代码中使用 TextView 的方法和属性。
相关问题
android中TextView k = findViewById(R.id.k);的k怎么实现动态赋值
在Android中,TextView k = findViewById(R.id.k);的k可以通过调用setText()方法来实现动态赋值。例如,如果要将k的文本设置为“Hello World”,可以使用以下代码:
k.setText("Hello World");
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了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.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)