0

I have one spinner,text field and a button. On click of the button, the text entered in the texfield has to be added to the spinner. I have used ArrayList to add the text. But the application crashes on click of the button.The other two spinners are just dummy.

The Activity

public class MainActivity extends ActionBarActivity {
     Spinner spinner1,spinner2,spinner3;
     Button add;
     EditText subject;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     // Spinner element
       spinner1 = (Spinner) findViewById(R.id.spinner1);
       spinner2 = (Spinner) findViewById(R.id.spinner2);
       spinner3 = (Spinner) findViewById(R.id.spinner3);
       add = (Button) findViewById(R.id.button1);
       subject = (EditText) findViewById(R.id.editText1);

    // Spinner click listener

       //spinner2.setOnItemSelectedListener(this);
       //spinner3.setOnItemSelectedListener(this);
       spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

           @Override
           public void onItemSelected(AdapterView<?> adapter, View v,
                   int position, long id) {
               // On selecting a spinner item
               String item = adapter.getItemAtPosition(position).toString();

               // Showing selected spinner item
               Toast.makeText(getApplicationContext(),
                       "Selected Subject : " + item, Toast.LENGTH_LONG).show();
           }

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

           }
       });
       add.setOnClickListener(new OnClickListener() {


         Context context;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String content = subject.getText().toString();
            List list = new ArrayList();
            list.add(content);
            ArrayAdapter dataAdapter = new ArrayAdapter(context,android.R.layout.simple_spinner_item, list);

            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                spinner1.setAdapter(dataAdapter);


        }
       });



        }
    public void addListenerOnSpinnerItemSelection() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
                //do something here
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                //optionally do something here
            }
        });
      }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

The XML is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="attended all classes??" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="169dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="" >
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="169dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Add"


        />

</LinearLayout>
user3359953
  • 47
  • 1
  • 8

2 Answers2

0

You declared context and not instantiated it, thus it is null. If you see NullPointerException in you LogCat try the following

ArrayAdapter dataAdapter = new ArrayAdapter(context,android.R.layout.simple_spinner_item, list);

So replace it with

ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, list);
makata
  • 2,188
  • 2
  • 28
  • 23
0

As stated in a comment, you should always post the logcat with your question when your app crashes. Here, it looks like the problem is that context is null. You declare it here

Context context;

but you never initialize it. But there is no need for this extra variable anyway as you can get the Context from the View clicked with v.getContext() inside your onClick().

ArrayAdapter dataAdapter = new ArrayAdapter(v.getContext(), 
        android.R.layout.simple_spinner_item, list);

See the docs

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Helpful Links

What is a NullPointerException

Unfortunately my app has stopped working

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93