Question

so I have this getter function in a Class called ItemEnergyGreen:

public TextView getTextView(FrameLayout fl) {
    textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);
    return textView;
}

And I want now to call this methode from another Class:

//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);

The function call is in Line 2, I am passing the frameLayout to the getter Function but textView in my ItemEnergyGreen class is always NULL.

The error occurs when i want to call setText on my view:

public void itemEnergyGreenUpdateNumber() {
    int quantity = itemEnergyGreen.getQuantity();
    String qstring = String.valueOf(quantity);
    itemEnergyGreenQuantityTextView.setText(qstring);
}

This is the XML-FrameLayout that is get inflated:

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

Why is that?

ps. At the point where the getter function is called, the itemEnergyGreen Object is already initialized.

Thank you for your answers, and sorry if that is a dump question but i´m going crazy about this.

Was it helpful?

Solution

textView in my ItemEnergyGreen class is always NULL.

findViewById looks for a view in the current view hierarchy. Probably TextView is not a child of FrameLayout. So initialization fails leading to NullPointerException.

The other reason could be that you referenced wrong id.

Edit:

Id referenced in wrong

<TextView
    android:id="@+id/textview_energyGreen_quantity"

Change this

textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);

to

textView = (TextView) fl.findViewById(R.id.textview_energyGreen_quantity);

OTHER TIPS

Try this

 private View mView;
 mView =inflater.inflate(R.layout.item_energy_green_layout, null, false);
 itemEnergyGreenQuantityTextView = itemEnergyGreen.getTextView(mView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top