首页 > 解决方案 > 无法解析方法'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);

标签: javaandroid

解决方案


您将 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()); 

推荐阅读