-2

So I am attempting to use a spinner to change the text colour of a message in a text view. However, when I attempt to use the function attached to my button click the activity crashes. I have attached the relevant part of the java code and the xml file

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;

public class Colour extends AppCompatActivity {

private Spinner mySpinner;
public static final String EXTRA_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_colour);
    Intent intent = getIntent();
    String messageText = intent.getStringExtra(EXTRA_MESSAGE);
    TextView messageView = (TextView)findViewById(R.id.message);
    messageView.setText(messageText);
}

public void change(View v){
    String text = mySpinner.getSelectedItem().toString();
    TextView newColour = (TextView) findViewById(R.id.message);
    if(text == "Red"){
        newColour.setTextColor(Color.RED);
    }
    if(text == "Blue"){
        newColour.setTextColor(Color.BLUE);
    }
    if(text == "Green"){
        newColour.setTextColor(Color.GREEN);
    }
}

XML file:

<TextView
    android:textColor="#000000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/message" />

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/colours"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:id="@+id/spinner" />

<Button
    android:text="HOME"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:onClick="home"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="52dp" />

<Button
    android:text="changeColour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button4"
    android:layout_above="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_marginBottom="101dp"
    android:onClick="change" />
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
killiantos
  • 1
  • 1
  • 2

2 Answers2

2

mySpinner is not initialized before access it's method. please initialize mySpinner first. use following code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mySpinner = (Spinner)findViewById(R.id.spinner);

    }

public void change(View v){
        String text = mySpinner.getSelectedItem().toString();
        TextView newColour = (TextView) findViewById(R.id.message);
        if(text.equals("Red")){
            newColour.setTextColor(Color.RED);
        }
        if(text.equals("Blue")){
            newColour.setTextColor(Color.BLUE);
        }
        if(text.equals("Green")){
            newColour.setTextColor(Color.GREEN);
        }
    }
user7676575
  • 141
  • 6
0

it seems that you did not setup the spinner in the change method so you need to initialize it first

Omar El Halabi
  • 2,118
  • 1
  • 18
  • 26