java - 无法解析方法'setText(java.lang.String[])'
问题描述
任何人请告诉我我应该在哪里以及如何纠正这个
公共类 Book_Activity 扩展 AppCompatActivity {
private TextView tvtitle,tvdescription,tvcategory;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
tvtitle = findViewById(R.id.txttitle);
TextView tvdescription = findViewById(R.id.txtdes);
tvcategory = findViewById(R.id.txtcat);
img = findViewById(R.id.bookthumbnail);
// Recieve data
Intent intent = getIntent();
String Title = intent.getExtras().getString("Title");
String Category = intent.getExtras().getString("Category");
String[] Description = intent.getExtras().getStringArray("Description");
int image = intent.getExtras().getInt("Thumbnail") ;
// Setting values
tvtitle.setText(Title);
tvcategory.setText(Category);
tvdescription.setText(Description);
img.setImageResource(image);
解决方案
您将 Description 作为string []
tptvdescription
文本视图传递,但文本视图仅支持string
. 那是个问题。
所以,首先你需要纠正这个过程。例如,您需要从您使用的字符串数组中确定哪些数据需要在文本视图中显示。
或者,
如果要在文本视图上显示字符串数组,则应将该字符串数组设置为字符串。为此,您可以尝试以下解决方案,
解决方案 1
String[] Description = { "first", "second", "third"}
// Print as [first, second, third ]
tvdescription.setText(Arrays.toString(Description));
// Print as first, second, third
tvdescription.setText(Arrays.toString(Description).replaceAll("\\[|\\]", ""));
解决方案 2
String[] Description = { "first", "second", "third"}
StringBuilder descriptionText = new StringBuilder();
for (String s: Description) {
descriptionText.append(s);
descriptionText.append(" ");
}
tvdescription.setText(descriptionText.toString().trim());
推荐阅读
- firebase - Google API sendVerificationCode 返回 backendError 39
- reactjs - 如何自定义宽度
- javascript - 将鼠标悬停在孩子上时防止触发 onMouseOut 事件
- javascript - Styleguidist 无法与 ReactDOM.render() 一起正常工作
- c# - C#在派生类中使用基类的重载二元运算符(+、*、/、-)运算符
- python - 邮寄请求中的回车
- python - 如何在特定字段上使用排序
- apache-zeppelin - Apache Zeppelin 0.9.0 - Helium 组件列表为空
- .htaccess - 我的 .htaccess 在我的主登录页面上工作,但随后的重定向不起作用
- c++ - 使用 java 8 与 JNI.h 函数 impl_JNI_GetDefaultJavaVMArgs 的 C++ 链接错误