繁体   English   中英

如果再次选择项目,则调整器

[英]spinner if item selected again

我在我的包中创建了一个名为NoDefaultSpinner的类文件。 但是,当我尝试在我的Android应用程序中使用它时,我得到一个异常会导致程序崩溃。

我从这个链接复制了Dimitar Vukman和Blundell提供的代码,然后更新了这段代码,因为Alex Miragall指示了一些帖子(因为现在我正在为Android 4.0创建一个应用程序)。 我删除了Dimitar的onClick方法,并在课程结束时粘贴了Alex的代码。

然后我用旋转器编辑了我的活动,我改变了

static Spinner spinner1;

static NoDefaultSpinner spinner1;

variables.spinner1 = (Spinner) findViewById(R.id.spinner1); 

variables.spinner1 = (NoDefaultSpinner) findViewById(R.id.spinner1); 

我也试着改变

<Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/place" />

<NoDefaultSpinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/place" />

问题:我做错了什么,为什么我的应用程序崩溃了,我该怎么做才能让它正常工作?

如果我不更新布局,这些是错误:

01-08 01:50:48.835: E/AndroidRuntime(13362): FATAL EXCEPTION: main
01-08 01:50:48.835: E/AndroidRuntime(13362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.life/com.example.life.search_activity}: java.lang.ClassCastException: android.widget.Spinner cannot be cast to com.example.life.NoDefaultSpinner
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.os.Looper.loop(Looper.java:137)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at java.lang.reflect.Method.invoke(Method.java:511)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at dalvik.system.NativeStart.main(Native Method)
01-08 01:50:48.835: E/AndroidRuntime(13362): Caused by: java.lang.ClassCastException: android.widget.Spinner cannot be cast to com.example.life.NoDefaultSpinner
01-08 01:50:48.835: E/AndroidRuntime(13362):    at com.example.life.search_activity.onCreate(search_activity.java:181)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.Activity.performCreate(Activity.java:5104)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-08 01:50:48.835: E/AndroidRuntime(13362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-08 01:50:48.835: E/AndroidRuntime(13362):    ... 11 more

如果我更新布局(将Spinner更改为NoDefaultSpinner),这些都是错误

01-08 01:53:22.803: E/AndroidRuntime(13417): FATAL EXCEPTION: main
01-08 01:53:22.803: E/AndroidRuntime(13417): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.life/com.example.life.search_activity}: android.view.InflateException: Binary XML file line #7: Error inflating class NoDefaultSpinner
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.os.Looper.loop(Looper.java:137)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at java.lang.reflect.Method.invoke(Method.java:511)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at dalvik.system.NativeStart.main(Native Method)
01-08 01:53:22.803: E/AndroidRuntime(13417): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class NoDefaultSpinner
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.Activity.setContentView(Activity.java:1881)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at com.example.life.search_activity.onCreate(search_activity.java:177)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.Activity.performCreate(Activity.java:5104)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-08 01:53:22.803: E/AndroidRuntime(13417):    ... 11 more
01-08 01:53:22.803: E/AndroidRuntime(13417): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.NoDefaultSpinner" on path: /data/app/com.example.life-1.apk
01-08 01:53:22.803: E/AndroidRuntime(13417):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-08 01:53:22.803: E/AndroidRuntime(13417):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-08 01:53:22.803: E/AndroidRuntime(13417):    ... 21 more

我在堆栈上找到了两个解决问题的方法:

首先

第二

我有同样的问题,有人问这些问题。 那里的问题很好。

我尝试了所有解决方案,但每次我尝试使用这些特殊的微调器启动活动应用程序崩溃。 也许我确实使用了错误的类?

你的第一个例外是由

java.lang.ClassNotFoundException: Didn't find class "android.view.NoDefaultSpinner" on path: /data/app/com.example.life-1.apk

因此,在xml布局中使用完全限定名称( com.myPackage.NoDefaultSpinner )。 否则,Android假定该类是其自己的包的一部分,它显然会失败。

这个:

ClassCastException: android.widget.Spinner cannot be cast to com.example.life.NoDefaultSpinner

建议NoDefaultSpinner不扩展Spinner ,确保它是。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM