user1956641
user1956641

Reputation: 99

spinner if item selected again

I created a class file in my package, called NoDefaultSpinner. When I try to use it in my Android app, though, I get an exception that crashes the program.

I copied the code provided by Dimitar Vukman and Blundell from this link and then updated this code as Alex Miragall instructed some posts down (because now I'm creating an application for Android 4.0). I removed Dimitar's onClick method and pasted Alex's code at the end of the class.

Then I edited my activity with spinners, I changed

static Spinner spinner1;

to

static NoDefaultSpinner spinner1;

and

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

to

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

Also I tried to change

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

to

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

The question: what I did wrong, why my app crashes and what can I do to make it work properly?

These are the errors if i don't update layout:

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

These are errors if I do update the layout (change Spinner to 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

I have found two solutions for my problem here on stack:

The first

The second

I have the same problem that had people who asked these questions. The problem is well presented there.

I tried all the solutions, but every time I tried to launch an activity with these special spinners application crashed. Maybe I did use the classes wrong?

Upvotes: 1

Views: 636

Answers (1)

A--C
A--C

Reputation: 36449

Your first exception is caused by

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

So use the fully qualified name (com.myPackage.NoDefaultSpinner) in your xml layout. Otherwise, Android assumes the class is part of its own package and it will obviously fail.

Also, this:

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

Suggests that NoDefaultSpinner does not extend Spinner, make sure it is.

Upvotes: 3

Related Questions