Search code examples
nullpointerexceptionandroid-spinnerandroidandroid-event

how to get Spinner List's selected items's text and fill EditText in android with that value


when I try to get the EditText object in the Itemselected function of selection listener the line throws NullPointerException

<resources>
<string-array name="gender">
    <item name="Male">Male</item>
    <item name="Female">Female</item>
</string-array>
</resources>

layout

<EditText
    android:id="@+id/answer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textview1"
    android:layout_below="@+id/textview1"
    android:ems="10"
    android:hint="answer" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/answer"
    android:layout_below="@+id/answer"
    android:layout_marginTop="22dp"
    android:entries="@array/gender"
    android:prompt="@string/spin_prompt" />

Main Activity

public class MenuActivity extends Activity {

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

    Spinner sp= (Spinner) findViewById(R.id.spinner1);
    sp.setOnItemSelectedListener(new SpinnerActivity());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}


}

Spinner Activity

public class SpinnerActivity extends Activity implements OnItemSelectedListener{

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

                String selec = (parent.getItemAtPosition(pos)).toString();

                EditText ans= (EditText) findViewById(R.id.answer); 
                //NullPointerException at the time of retrieving EditText object 

                ans.setText(selec);
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

}

Log-Cat Error Entries

03-21 17:23:42.232: E/AndroidRuntime(4019): FATAL EXCEPTION: main
03-21 17:23:42.232: E/AndroidRuntime(4019): java.lang.NullPointerException
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.app.Activity.findViewById(Activity.java:1853)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at com.practice.menu.SpinnerActivity.onItemSelected(SpinnerActivity.java:19)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.widget.AdapterView.access$200(AdapterView.java:49)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.os.Handler.handleCallback(Handler.java:730)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.os.Looper.loop(Looper.java:137)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at java.lang.reflect.Method.invoke(Method.java:525)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 17:23:42.232: E/AndroidRuntime(4019):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • Here:

    sp.setOnItemSelectedListener(new SpinnerActivity());
    

    You are trying to instantiate another activity and you try to set it as the listener. That won't work because Android Activities don't like it to get instantiated explicitly.

    Try implementing listener for your spinner like this:

        sp.setOnItemSelectedListener(new OnItemSelectedListener() {
    
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
    
            }
    
        });
    

    the SpinnerActivity you should not need then anymore