the_big_blackbox the_big_blackbox - 5 months ago 25
Android Question

text view returning null

I have 2 classes BuyCoins and CustomOnItemSelectListerner. The BuyCoins class contains a spinner and once the spinner is selected the CustomOnItemSelectListerner class is called.

public class BuyCoins extends AppCompatActivity {
...
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
//TextView t=(TextView) findViewById(R.id.conversion);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener(t));
}
...
}

public class CustomOnItemSelectedListener implements OnItemSelectedListener {
//private TextView t;

//CustomerOnItemSelected (TextView V) {t=V;}

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

String selected= parent.getItemAtPosition(pos).toString();
TextView t=(TextView)parent.findViewById(R.id.conversion);

switch(selected) {
case "A":
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + selected,
Toast.LENGTH_SHORT).show();
t.setText("$ala$");
break;
}
...
}


My issue is that t.setText returns null, however the rest of the case statement works fine. Please help.

Layout file with the spinner

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50dp"
android:paddingTop="50dp"
android:paddingRight="50dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Purchase Coins"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />


<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/coinpic"
android:src="@drawable/coin"
android:scaleX="0.8"
android:scaleY="0.8"
android:layout_gravity="center_horizontal" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/coin_array"
android:prompt="@string/coin_prompt" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/conversion"
android:layout_gravity="right" />

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Purchase Coins"
android:id="@+id/Pur_button"
android:layout_gravity="center_horizontal"
android:onClick="toast"/>

</LinearLayout>

Answer

The TextView you're trying to acceas belongs to your overall layout, not the spinner, so you'd need to access it from your Activity using:

TextView t=(TextView) findViewById(R.id.conversion);

You could pass it to your listener by creating a new constructor in your CustomOnItemSelectListener that takes a TextView which you can set to a member variable which you then call setText on below.

Then when creating the listener in your Acticity you call the new constructor to pass in the TextView:

    new CustomOnItemSelectListener(t);