I have one spinner in which few values are there from strings.xml and I want to populate that spinner dynamically from edit text using onclick event of Button but somehow it's not working and I am getting Force Close everytime. Any help will be appreciated.
package com.example.expense;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class Expense3 extends Activity {
//private TextView t1;
private Spinner spinner;
private EditText Text;
private ArrayAdapter<CharSequence> adapter;
private Button addButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Text = (EditText)findViewById(R.id.widget4);
addButton = (Button)findViewById(R.id.add_new);
spinner = (Spinner) findViewById(R.id.hhj);
adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
this.addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addNewSpinnerItem();
}
});
}
protected void addNewSpinnerItem() {
// TODO Auto-generated method stub
CharSequence textHolder = "" + Text.getText().toString();
adapter.add(textHolder);
}
}
Logcat error:-
05-14 01:07:19.934: ERROR/AndroidRuntime(802): FATAL EXCEPTION: main
05-14 01:07:19.934: ERROR/AndroidRuntime(802): java.lang.UnsupportedOperationException
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.util.AbstractList.add(AbstractList.java:411)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.util.AbstractList.add(AbstractList.java:432)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.example.expense.Expense3.addNewSpinnerItem(Expense3.java:50)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.example.expense.Expense3$1.onClick(Expense3.java:40)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.view.View.performClick(View.java:2408)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.view.View$PerformClick.run(View.java:8816)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.os.Handler.handleCallback(Handler.java:587)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:123)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:521)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-14 01:07:19.934: ERROR/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)