(数据绑定没问题。我使用的是android 4.2)
单击时,会出现以下错误。
我已尝试使用以下选项: 1 GT; C:\ Windows \ System32 \ drivers \ etc \ hosts文件添加127.0.0.1 localhost 2>在eclipse窗口中更改 - >首选项 - > Android-> DDMS设置端口8601并选中使用ADBHOST选项。(使用此选项我来了" vm不提供监视器信息"问题但是主线程终止了。)
我也试过不同的方法,但没有任何帮助。 我们将非常感谢您的帮助。
主要活动代码:
package com.example.newapp;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner ss=(Spinner)findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
list.add("list 4");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(R.id.spinner1);
ss.setAdapter(dataAdapter);
ss.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
ss.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@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;
}
}
XML布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp" />
</RelativeLayout>
Logcat中的错误消息:
03-31 20:19:56.569: E/AndroidRuntime(7110): FATAL EXCEPTION: main
03-31 20:19:56.569: E/AndroidRuntime(7110): android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2136)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.content.res.Resources.getLayout(Resources.java:865)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:741)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:737)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.Spinner.measureContentWidth(Spinner.java:685)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.Spinner$DropdownPopup.show(Spinner.java:964)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.widget.Spinner.performClick(Spinner.java:608)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.view.View$PerformClick.run(View.java:17318)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.os.Handler.handleCallback(Handler.java:615)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.os.Looper.loop(Looper.java:137)
03-31 20:19:56.569: E/AndroidRuntime(7110): at android.app.ActivityThread.main(ActivityThread.java:4921)
03-31 20:19:56.569: E/AndroidRuntime(7110): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 20:19:56.569: E/AndroidRuntime(7110): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 20:19:56.569: E/AndroidRuntime(7110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-31 20:19:56.569: E/AndroidRuntime(7110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-31 20:19:56.569: E/AndroidRuntime(7110): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您面临的问题来自这一行:
dataAdapter.setDropDownViewResource(R.id.spinner1);
您正在尝试将ID设置为所有微调器项目的布局,这显然是不允许的。
如果您的目的是为下拉列表项目实现不同的样式,则可以使用方法setDropDownViewResource();
并传递有效的布局ID。否则,只需删除此行,您的微调器就会使用默认主题显示。