0

我有一个带有一些按钮和 TextView(称为 Row2Col1、Row2Col3、Row2Col4)的表,我想从动态生成的字符串中获取它们的 id。代码是:

String index = "Row" + (currentRow + 2) + "Col1";
    Button buttonTable = (Button) findViewById(getResources()
            .getIdentifier(index, "id", getPackageName()));
    buttonTable.setVisibility(0);

    index = "Row" + (currentRow + 2) + "Col3";
    TextView textViewBrand = new TextView(getApplicationContext());
    textViewBrand = (TextView) findViewById(getResources().getIdentifier(
            index, "id", getPackageName()));
    textViewBrand.setText(brand);

    index = "Row" + (currentRow + 2) + "Col4";
    TextView textViewTread = new TextView(getApplicationContext());
    textViewTread = (TextView) findViewById(getResources().getIdentifier(
            index, "id", getPackageName()));
    textViewTread.setText(tread);

程序在第二行崩溃,findViewById 在哪里。有任何想法吗?

4

1 回答 1

2

根据您的错误文本,您在 xml 中有 TextView,但您正试图将其转换为代码中的 Button。将您的代码更改为

String index = "Row" + (currentRow + 2) + "Col1";
    TextView buttonTable = (TextView) findViewById(getResources()
            .getIdentifier(index, "id", getPackageName()));
    buttonTable.setVisibility(0);

它会起作用。

于 2013-06-14T14:31:11.620 回答