这里我有一个很好的简单表格布局来显示我的数据库的内容。
<?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/