我正在尝试使用一个微调器来构建一个应用程序。
该应用程序已启动,一切正常,直到我按下微调器然后崩溃
收到此消息:
"ArrayAdapter requires the resource ID to be a text view"
这是我的代码:
public class MainActivity extends ActionBarActivity {
private Spinner s;
String substance;
CharSequence substances[] = {"PenicillinV", "Amoxicillin", "Ampicillin"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
s = (Spinner) findViewById( R.id.substancesSpinner );
ArrayAdapter<CharSequence> adapter = null;
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, substances );
adapter.setDropDownViewResource( android.R.layout.simple_list_item_2 );
s.setAdapter( adapter );
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view, int pos, long id){
int index = s.getSelectedItemPosition();
//storing string resources in an array
substance = s.getSelectedItem().toString();
Log.i("MyActivity", "substance " + substance);
Toast.makeText(getBaseContext(), "You have selected : " +substances[index], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0){
//DoNothing
}
});
}
这是日志:
04-10 17:04:17.922: E/ArrayAdapter(475): You must supply a resource ID for a TextView
04-10 17:04:17.922: D/AndroidRuntime(475): Shutting down VM
04-10 17:04:17.922: W/dalvikvm(475): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-10 17:04:17.972: E/AndroidRuntime(475): FATAL EXCEPTION: main
04-10 17:04:17.972: E/AndroidRuntime(475): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.ListView.onMeasure(ListView.java:1109)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.View.measure(View.java:8171)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.View.measure(View.java:8171)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-10 17:04:17.972: E/AndroidRuntime(475): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.View.measure(View.java:8171)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.View.measure(View.java:8171)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.View.measure(View.java:8171)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.os.Looper.loop(Looper.java:123)
04-10 17:04:17.972: E/AndroidRuntime(475): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-10 17:04:17.972: E/AndroidRuntime(475): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 17:04:17.972: E/AndroidRuntime(475): at java.lang.reflect.Method.invoke(Method.java:521)
04-10 17:04:17.972: E/AndroidRuntime(475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-10 17:04:17.972: E/AndroidRuntime(475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-10 17:04:17.972: E/AndroidRuntime(475): at dalvik.system.NativeStart.main(Native Method)
04-10 17:04:17.972: E/AndroidRuntime(475): Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem
04-10 17:04:17.972: E/AndroidRuntime(475): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
04-10 17:04:17.972: E/AndroidRuntime(475): ... 34 more
04-10 17:04:18.082: D/dalvikvm(475): GC_FOR_MALLOC freed 2976 objects / 266592 bytes in 97ms
04-10 17:04:20.142: I/Process(475): Sending signal. PID: 475 SIG: 9
答案 0 :(得分:1)
android.R.layout.simple_dropdown_item_1line而不是android.R.layout.simple_list_item_2会解决您提到的第一个问题。
对于第二个问题,我在过去遇到了正确的主题下拉菜单问题。特别是在使用带有光动作条的暗色时。有时您必须使用主题上下文。如果它在操作栏中,请尝试使用
new ArrayAdapter<CharSequence>(getActionBar().getThemedContext()
,android.R.layout.simple_spinner_item, substances );
否则我建议您为下拉项目创建自己的布局,并将android.R.layout.simple_dropdown_item_1line替换为您创建的布局。