0

The problem Im having is with the onItemSelected method. Whenever I try to change a Views value, Example: TextView, EditView it throws an error.

What Im trying to do is get the when I click on something on the spinner it will display something on the text (anything currently). I hope you can help! Thanks.

Here is my code so you can look at it:

public class MainActivity extends Activity implements OnItemSelectedListener {

    Spinner spinner;
    String[] paths = {"Rectangle", "Circle", "Triangle"};
    String selected;
    Button equals;
    TextView parm1, parm2;
    EditText value1, value2;
    CheckBox checkArea, checkPerm;
    Boolean checkedArea = true, checkedPerm = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, paths);
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        int index = arg0.getSelectedItemPosition();

        if(paths[index] == "Rectangle"){
            //Does work
            Toast.makeText(getBaseContext(), "You selected Rectangle", Toast.LENGTH_LONG).show();

            //Doesn't work
            parm1.setText("Hello");
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

Here is the error that pops up when I run the program. The virtual device will run the program all the way up to when i open it, then it will say Unfortunately *"Programs_name"* has stopped working. Here is the error log:

 02-12 17:26:41.395: D/dalvikvm(1478): GC_FOR_ALLOC freed 45K, 4% free 3331K/3444K, paused 47ms, total 51ms
02-12 17:26:41.985: D/AndroidRuntime(1478): Shutting down VM
02-12 17:26:41.985: W/dalvikvm(1478): threadid=1: thread exiting with uncaught exception (group=0xb1aadba8)
02-12 17:26:41.995: E/AndroidRuntime(1478): FATAL EXCEPTION: main
02-12 17:26:41.995: E/AndroidRuntime(1478): Process: com.example.gcfcalculator, PID: 1478
02-12 17:26:41.995: E/AndroidRuntime(1478): java.lang.NullPointerException
02-12 17:26:41.995: E/AndroidRuntime(1478):     at com.example.gcfcalculator.MainActivity.onItemSelected(MainActivity.java:53)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.widget.AdapterView.access$200(AdapterView.java:48)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.os.Handler.handleCallback(Handler.java:733)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.os.Looper.loop(Looper.java:136)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at java.lang.reflect.Method.invoke(Method.java:515)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-12 17:26:41.995: E/AndroidRuntime(1478):     at dalvik.system.NativeStart.main(Native Method)
02-12 17:26:47.055: I/Process(1478): Sending signal. PID: 1478 SIG: 9
1
  • 2
    You never initialize parm1. Also use equals() to compare String values.
    – Alexis C.
    Feb 12, 2014 at 22:39

1 Answer 1

1

You have no reference to the TextView parm1, to get a reference you must first call findViewById as follows:

parm1 = (TextView) findViewById(R.id.yourId);

Until you do that, you will be able to use that textView, also, when comparing Strings do not use == instead go for Object.equal(), and just as a small useful tip, if you have a literal string instead of doing

paths[index].equals("Rectangle")

do

"Rectangle".equals(paths[index])

This way you have an extra validation for null pointer exception, because your literal could never be null, however theres a chance that "paths[index]" might be null

Hope it Helps!

Regards!

2
  • Thanks man! It works perfectly! I cant believe I forgot to reference the TextView parm1. I really appericate it. Feb 13, 2014 at 1:51
  • @user3303817 If you like his answer, don't forget to Accept it. Feb 13, 2014 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.