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

标签 android xml sqlite android-layout

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

<?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>

这是在调用 Activity 的 onCreate 中调用的方法:

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() 时,问题出现了。这只是一个常量还是“数据”变量。 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;

关于android - 如何在android嵌入式卡中设置文本(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28770004/

相关文章:

android - 在 Jetpack Compose 中,如何应用存储在 Assets 中的字体/字体?

java - 关于 didRangeBeaconsInRegion

android - MySQL 数据库连接

Android xml sp, dp, px 问题

javascript - 多个字符串的单个 XPath?

android - 应用程序在启动时意外停止

Android 键盘弹出字符不显示

sql - 复合主键限制?

c# - sqlite 在 C# 中比较时间戳和 DateTime

c# - 如何以WAL方式打开SQLite连接