Eclipse ClassCastException when trying to extend XML layout for Android

I am new to java, eclipse and android development, so I can skip something simple, although I have checked several basic things like spelling several times.

Since the Android SDK does not have a number selection control (such as that used in the date picker), the usual wisdom is to “clone and own” a control that exists in the Android source.

In an attempt to do this, I copied the code for NumberPicker to my project and its dependency, NumberPickerButton. I also copied auxiliary resources.

The problem is that when I try to include NumberPicker in the layout, the rendering version of the layout inside Eclipse does not work.

<com.spencerandbrown.PatientTracker1.NumberPicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spanEnd2" /> 

Adding this tag causes the Layout view of the XML file to display only the following error.

 ClassCastException: com.spencerandbrown.PatientTracker1.NumberPickerButton cannot be cast to com.spencerandbrown.PatientTracker1.NumberPickerButton 

I tried both the full name and the unqualified name, and that doesn't make any difference.

It’s strange that the control works at runtime, it just won’t appear in Eclipse.

Is there a solution for this problem? Am I just doing something wrong?

If necessary, I can include more code from my project in the code to clarify.

If I refer to the internal Android widget, it works fine in the Eclipse layout, but it is inconvenient to interact with this path, and this may break in the future.

 <com.android.internal.widget.NumberPicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spanStart"/> 

Here is the stack trace from Eclipse.

 java.lang.ClassCastException: com.spencerandbrown.PatientTracker1.NumberPickerButton cannot be cast to com.spencerandbrown.PatientTracker1.NumberPickerButton at com.spencerandbrown.PatientTracker1.NumberPicker.<init>(NumberPicker.java:110) at com.spencerandbrown.PatientTracker1.NumberPicker.<init>(NumberPicker.java:98) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:532) at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(Unknown Source) at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(Unknown Source) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:198) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:126) at android.view.LayoutInflater.rInflate(LayoutInflater.java:617) at android.view.LayoutInflater.rInflate(LayoutInflater.java:620) at android.view.LayoutInflater.rInflate(LayoutInflater.java:620) at android.view.LayoutInflater.inflate(LayoutInflater.java:407) at android.view.LayoutInflater.inflate(LayoutInflater.java:296) at com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:377) at com.android.ide.eclipse.adt.internal.editors.layout.GraphicalLayoutEditor.computeLayout(Unknown Source) at com.android.ide.eclipse.adt.internal.editors.layout.GraphicalLayoutEditor.recomputeLayout(Unknown Source) at com.android.ide.eclipse.adt.internal.editors.layout.GraphicalLayoutEditor.activated(Unknown Source) at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(Unknown Source) at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:291) at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1176) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1200) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1185) at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1025) at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3256) at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:2045) at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:323) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1176) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3493) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3112) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405) at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369) at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514) at org.eclipse.equinox.launcher.Main.run(Main.java:1311) 
+4
source share
2 answers

Workaround: I found that if I catch and ignore ClassCastException and obviously then I am not trying to do anything with the View that I was trying to find, there are no more problems, and I can use the layout editor again: the editor will pretty happily load, to initialize and draw a problematic representation, you simply cannot pass it to the class.

 try { picker = (NumberPicker) findViewById(R.id.myPicker); picker.blargh(); // everything else involving picker } catch (ClassCastException issue6894) {} // ignore 
+2
source

If this seems like an error in http://code.google.com/p/android/issues/detail?id=6894 (i.e. the user view loads another user view), then the problem is most likely related to the user loader classes that loads custom view classes.

you can use View.isInEditMode (it returns only true when your code works inside eclipse) if you want to distinguish correctly between running inside Eclipse or inside Android.

+3
source

All Articles