【问题标题】:Whats going on here? Textview cannot be applied to an int?这里发生了什么? Textview不能应用于int?
【发布时间】:2016-01-31 04:04:55
【问题描述】:

我正在创建一个 textview 数组并尝试在 android studio 中引用它,但它告诉我 textview 不能应用于 int??

TextView[]titles=new TextView[5];
    titles[1]=new TextView(R.id.dummy_text);

【问题讨论】:

  • 你的 R,id,dummy_text 是什么,它是你布局中的文本视图吗?如果是这样,您不能将其分配给 textview 的构造函数。如果您需要以编程方式创建 textivew,那么@Ameya Pandilwar 的答案是正确的。

标签: android arrays textview int


【解决方案1】:

按照这个:

TextView[] textViews = new TextView[5];
for(int i = 0; i < 5; i++)
    textViews[i] = new TextView(this);
textViews[0].setText(getResources().getString(R.string.dummy_text));
textViews[0].setId(R.id.dummy_text); // You can also use any integer value like 1, 56, 101, etc.

另请注意,您需要在 values 文件夹中的 ids.xml 中添加一个虚拟 id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2021-08-04
    • 1970-01-01
    • 2023-03-12
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多