的下拉列表中我有一個微調器,文本字段和一個按鈕。點擊該按鈕後,必須將在texfield中輸入的文本添加到微調器中。我用ArrayList來添加文本。但是,點擊按鈕時,應用程序崩潰。另外兩個spinners只是虛擬的。我需要將文本字段中的值添加到android
的活動
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;
}
}
}
的XML如下:
<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>
感謝您的提問。如果您可以在錯誤上發佈您的Logcat堆棧跟蹤,那麼我們可以給您一個更準確的答案 – drees
'context'爲'null'。你可以使用'v.getContext()' – codeMagic