我正在嘗試創建一個簡單微調,但顯示錯誤。我在我的項目中創建了一個活動。微調不工作
我的MainActivity類別:
public class MainActivity extends AppCompatActivity {
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> list = new ArrayList<String>();
list.add("K");
list.add("J");
list.add("S");
list.add("B");
list.add("G");
list.add("F");
list.add("G");
list.add("D");
list.add("C");
list.add("M");
sp=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.activity_main,list);
adapter.setDropDownViewResource(R.layout.activity_main);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),"You are selected "+label,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
這裏是我的activity_main.xml中,其中i增加了微調:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.firebase.ashik.dropdownlistviewvaluecount.MainActivity">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="81dp"
android:id="@+id/spinner" />
</RelativeLayout>
,並顯示該致命錯誤:
E/AndroidRuntime:致命例外:main java.lang.IllegalStateException:ArrayAdapter要求資源ID爲TextView
[「ArrayAdapter需要資源ID是TextView」的xml問題]的可能重複(http://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be- a-textview-xml-problems) –
adapter.setDropDownViewResource(R.layout.activity_main);刪除此行 –
你錯了充氣佈局,改變這種 'ArrayAdapter適配器=新ArrayAdapter (這一點,R.layout.activity_main,清單);' 這個 'ArrayAdapter 適配器=新ArrayAdapter ( this,R.layout.simple_spinner_item,list);' –