我有一个非常简单的应用程序,带有edittext,一个按钮和一个微调器。当我尝试创建一个新的Array Adapter时,我的程序崩溃了以下logcat:
06-14 02:48:23.082: E/AndroidRuntime(1065): FATAL EXCEPTION: main
06-14 02:48:23.082: E/AndroidRuntime(1065): android.content.res.Resources$NotFoundException: Resource ID #0x7f090002 type #0x12 is not valid
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.content.res.Resources.getLayout(Resources.java:853)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.Spinner.onMeasure(Spinner.java:439)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.View.measure(View.java:15518)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.View.measure(View.java:15518)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.View.measure(View.java:15518)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.View.measure(View.java:15518)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-14 02:48:23.082: E/AndroidRuntime(1065): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.View.measure(View.java:15518)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.os.Handler.handleCallback(Handler.java:725)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.os.Looper.loop(Looper.java:137)
06-14 02:48:23.082: E/AndroidRuntime(1065): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-14 02:48:23.082: E/AndroidRuntime(1065): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 02:48:23.082: E/AndroidRuntime(1065): at java.lang.reflect.Method.invoke(Method.java:511)
06-14 02:48:23.082: E/AndroidRuntime(1065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-14 02:48:23.082: E/AndroidRuntime(1065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-14 02:48:23.082: E/AndroidRuntime(1065): at dalvik.system.NativeStart.main(Native Method)
我认为是因为这段代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locales.put("US", Locale.US);
locales.put("Canada", Locale.CANADA);
locales.put("French-Canada", Locale.CANADA_FRENCH);
locales.put("Chinese", Locale.CHINA);
List<String> lArr = new ArrayList<String>();
for(String s : locales.keySet())
lArr.add(s);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.spinner, android.R.layout.simple_spinner_item, lArr);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
这应该是一件非常简单的事情。我偏离了android教程,创建了一个在代码中定义的数组,但似乎无法让它工作。
以下是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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="95dp"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:text="Button" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp" />
</RelativeLayout>
答案 0 :(得分:1)
由于您使用的是用于微调器的xml布局,因此不支持它,或者您将错误的参数传递给数组适配器的构造函数。
而不是:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.spinner, android.R.layout.simple_spinner_item, lArr);
使用:
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);