1

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?

4
  • 2
    You might need to do <com.mypackage.NoDefaultSpinner (So use the full name of the Class). Also, post your LogCat, so everyone can see exactly what the problem is. Commented Jan 8, 2013 at 1:44
  • I call this class from the same package, there is no need to use the full name. I'll write logs in a few minutes Commented Jan 8, 2013 at 1:47
  • 2
    Yes there is, when you're using it in XML. As @A--C says, use the fully qualified package name when referencing your custom View in XML. Commented Jan 8, 2013 at 1:58
  • 2
    This is because when you're writing XML for Android apps, the parser always assumes that the default namespace for XML tags is somewhere in android. In your Java, code, though, you're right, you don't need fully qualified names for things in the same package. Commented Jan 8, 2013 at 2:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, great, I've changed <NoDefaultSpinner in my xml with <com.example.life.NoDefaultSpinner, now it works, but with one exeption, reacts only after the second click on the element already selected. If I click on an element that is not selected already everithing is fine. Maybe it is so because I use a counter to check if the element selected by user or not, like so: i=0; ... public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { CharSequence t2 = (CharSequence) parent.getItemAtPosition(position); if(i>0){ ... } if(i==0) i++;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.