0

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>
the_big_blackbox
  • 1,056
  • 2
  • 15
  • 35

1 Answers1

1

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);
Karen Forde
  • 1,117
  • 8
  • 20
  • thanks but did not work.. it does not even allow me to select the spinner without bombing out with the same error – the_big_blackbox Jul 03 '16 at 12:37
  • I've changed my answer based on the xml you posted, sorry I misunderstood and thought the conversion view was part of the spinner item layout. – Karen Forde Jul 03 '16 at 13:31
  • thanks so much it worked you are an absolute star... I have updated the above code with the changes I made - see the comments in original code... – the_big_blackbox Jul 03 '16 at 14:15