我有这个代码:
Spinner entrega=(Spinner)findViewById(R.id.comboentrega); //This is the line where it cracks
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayentrega, android.R.layout.simple_spinner_item);
// 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
entrega.setAdapter(adapter);
在布局中:
<Spinner
android:id="@+id/comboentrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView4" />
当我运行该应用程序时,我会收到此堆栈跟踪:
03-13 11:43:13.748: E/AndroidRuntime(1668): FATAL EXCEPTION: main
03-13 11:43:13.748: E/AndroidRuntime(1668): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.publidirecta.AppAzafata/com.publidirecta.AppAzafata.Incidencias}: java.lang.ClassCastException: android.widget.Button
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.os.Looper.loop(Looper.java:130)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.ActivityThread.main(ActivityThread.java:3701)
03-13 11:43:13.748: E/AndroidRuntime(1668): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:43:13.748: E/AndroidRuntime(1668): at java.lang.reflect.Method.invoke(Method.java:507)
03-13 11:43:13.748: E/AndroidRuntime(1668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-13 11:43:13.748: E/AndroidRuntime(1668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-13 11:43:13.748: E/AndroidRuntime(1668): at dalvik.system.NativeStart.main(Native Method)
03-13 11:43:13.748: E/AndroidRuntime(1668): Caused by: java.lang.ClassCastException: android.widget.Button
03-13 11:43:13.748: E/AndroidRuntime(1668): at com.publidirecta.AppAzafata.Incidencias.onCreate(Incidencias.java:122)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 11:43:13.748: E/AndroidRuntime(1668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
如果我没有投射到 Button,为什么会收到 ClassCastException android.widget.Button?万一它可能有帮助,微调器首先连接到屏幕的右侧,并且工作得很好。我注意到它并将它附加到左侧,它必须在哪里,异常开始了。
有人对此有任何想法吗?谢谢你。