尝试扩展 Android 的 XML 布局时出现 Eclipse ClassCastException
我是 java、eclipse 和 android 开发的新手,所以尽管我已经多次检查过拼写等基本内容,但我可能会遗漏一些简单的内容。
由于公共 Android SDK 中没有数字选择器控件(例如日期选择器中使用的控件),因此 传统智慧是“克隆并拥有”Android源代码中存在的控件。
为了尝试做到这一点,我已将 NumberPicker 的代码复制到我的项目及其依赖项 NumberPickerButton 中。我还复制了支持资源。
我遇到的问题是,当我尝试将 NumberPicker 包含在布局中时,Eclipse 中布局的渲染版本不起作用。
<com.spencerandbrown.PatientTracker1.NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spanEnd2" />
添加该标记会导致 XML 文件的布局视图仅显示以下错误。
ClassCastException: com.spencerandbrown.PatientTracker1.NumberPickerButton cannot be cast to com.spencerandbrown.PatientTracker1.NumberPickerButton
我已经尝试过完全限定名称和非限定名称,但没有区别。
奇怪的是,该控件在运行时可以工作,只是不会在 Eclipse 中呈现。
这个问题有解决办法吗?我只是做错了什么吗?
如果有必要,我可以从我的项目中添加更多代码来进行澄清。
如果我引用内部 Android 小部件,它在 Eclipse 布局视图中工作正常,但以这种方式交互很笨拙,并且将来可能会崩溃。
<com.android.internal.widget.NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spanStart"/>
这是 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)
I am new to java, eclipse, and android development, so I may be missing something simple although I have checked basic stuff like spelling several times.
Because there is no number picker control (such as that used in the date picker) in the public Android SDK, the conventional wisdom is to "clone and own" the control that exists in the Android source.
In an attempt to do that, I have copied the code for NumberPicker into my project and its dependency, NumberPickerButton. I have also copied the supporting resources.
The problem I am having is that when I try to include the NumberPicker in a layout, the rendered version of the layout within Eclipse does not work.
<com.spencerandbrown.PatientTracker1.NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spanEnd2" />
Adding that tag causes the Layout view of the XML file to only show the following error.
ClassCastException: com.spencerandbrown.PatientTracker1.NumberPickerButton cannot be cast to com.spencerandbrown.PatientTracker1.NumberPickerButton
I have tried both the fully qualified name and the unqualified name and it makes no difference.
The strange thing is that the control works at run time, it just will not render in Eclipse.
Is there a solution for this issue? Am I just doing something wrong?
If necessary, I can include more code from my project to clarify.
If I reference the internal Android widget, it works fine in the Eclipse layout view, but it is clumsy to interact with that way and it might 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这与 http://code.google 上的错误类似。 com/p/android/issues/detail?id=6894
(即自定义视图加载另一个自定义视图),那么问题很可能是由于加载自定义视图类的自定义类加载器造成的。
如果您想正确区分在 Eclipse 内部运行还是在 Android 内部运行,可以使用 View.isInEditMode (仅当您的代码在 Eclipse 内部运行时返回 true)。
If this is similar to the bug at http://code.google.com/p/android/issues/detail?id=6894
(ie the custom view loads another custom view), then the problem is likely due to the custom class loader that loads the custom view classes.
you can use View.isInEditMode (only returns true when your code is running inside eclipse) if you want to properly differentiate between running inside Eclipse or inside Android.
解决方法:我发现,如果我捕获并忽略 ClassCastException,并且显然不要尝试对我试图查找的视图执行任何操作,则不会再出现任何问题,我可以再次使用布局编辑器:编辑器将很高兴地加载、初始化和绘制有问题的视图,你只是不能转换到它的类。
Workaround: I have found that if I catch and ignore the ClassCastException, and obviously then don't try to do anything with the View I was trying to find, there are no further problems and I can use the layout editor again: the editor will quite happily load, initialise and draw the problematic view, you just can't cast to its class.