如何在 android 嵌入式卡中设置文本()

how to setText() in android embedded card

这里我有一个很好的简单 table 布局来显示我的数据库的内容。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tableLayout1">

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date"
            android:id="@+id/Date"
            android:layout_weight="0.3333"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Systolic"
            android:id="@+id/Systolic"
            android:layout_weight="0.3333"
            android:textAlignment="center" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Diastolic"
            android:id="@+id/Diastolic"
            android:layout_weight="0.3333" />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/DateInfo"
            android:layout_weight="0.3333"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/SystolicInfo"
            android:layout_weight="0.3333"
            android:textAlignment="center" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/DiastolicInfo"
            android:layout_weight="0.3333" />
    </TableRow>
</TableLayout>

这里是在调用的onCreate中调用的方法activity:

private View buildView() {
    //CardBuilder card = new CardBuilder(this, CardBuilder.Layout.MENU);
    //card.setText("It worked!!!");
    CardBuilder previousReadings = new CardBuilder(this, CardBuilder.Layout.EMBED_INSIDE);
    previousReadings.setEmbeddedLayout(R.layout.blood_pressures);

    TextView tv = (TextView) findViewById(R.id.SystolicInfo);
    //tv.setText("Why won't this work?");
    MyDatabase info = new MyDatabase(this);
    info.open();
    String data = info.getData();
    info.close();
    System.out.println(data);
    //tv.setText(data);

    return previousReadings.getView();
    //return card.getView();
}
}

我使用了 CarBuilder 对象卡片,这是一张没有嵌入式布局的简单卡片,效果很好。然后我将其注释掉并尝试实现也可以正常工作的嵌入式卡(上图)。但是,当我尝试 tv.setText() 时,问题就出现了。这只是一个常量还是 "data" 变量。 logcat 显示“无法加载 Activity:... 空指针异常。

任何人都可以阐明这个问题吗?我已经尝试过 EditTexts 和 TextViews,但都不起作用。提前致谢。

使用 previousReadings.getView() 调用 findViewById 从 Card 访问 TextView:

....
System.out.println(data);
View view= previousReadings.getView();
TextView tv = (TextView)view.findViewById(R.id.SystolicInfo);
tv.setText("Why won't this work?");
return view;