【发布时间】:2014-03-29 18:38:11
【问题描述】:
所以我在一个名为 ItemEnergyGreen 的类中有这个 getter 函数:
public TextView getTextView(FrameLayout fl) {
textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);
return textView;
}
我现在想从另一个类调用这个方法:
//Item Energy Green
itemEnergyGreenFrameLayout = (FrameLayout) inflater.inflate(R.layout.item_energy_green_layout, null, false);
itemEnergyGreenQuantityTextView = itemEnergyGreen.getTextView(itemEnergyGreenFrameLayout);
btn_ItemEnergyGreen = (ImageButton) itemEnergyGreenFrameLayout.findViewById(R.id.btn_energyGreenItem_use);
btn_ItemEnergyGreen.setOnClickListener(this);
函数调用在第 2 行,我将 frameLayout 传递给 getter 函数,但我的 ItemEnergyGreen 类中的 textView 始终为 NULL。
当我想在视图上调用 setText 时发生错误:
public void itemEnergyGreenUpdateNumber() {
int quantity = itemEnergyGreen.getQuantity();
String qstring = String.valueOf(quantity);
itemEnergyGreenQuantityTextView.setText(qstring);
}
这是膨胀的 XML-FrameLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ItemEnergyGreenFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/btn_energyGreenItem_use"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/item_energydrink_green" >
</ImageButton>
<TextView
android:id="@+id/textview_energyGreen_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#F00"
android:clickable="false"
android:padding="2dp"
android:textColor="#FFF"
android:textStyle="bold" >
</TextView>
</FrameLayout>
这是为什么呢?
ps。在调用 getter 函数时, itemEnergyGreen 对象已经初始化。
感谢您的回答,如果这是一个垃圾问题,我很抱歉,但我要疯了。
【问题讨论】:
-
在调用之前是否已将文本设置为 textview?就像在你的 xml 中一样?甚至是textview.settext?还是空的?
-
请显示 XML 并记录错误
-
你确定
TextView属于item_energy_green_layout.xml。发布xml -
我已经发布了导致 NullPointerExeption 的 xml 和函数
-
@DannielR 看看我帖子中的编辑你引用了错误的 id
标签: android textview android-framelayout