努力扩大Android的XML布局当Eclipse ClassCastException异常 [英] Eclipse ClassCastException when trying to expand XML layout for Android

查看:174
本文介绍了努力扩大Android的XML布局当Eclipse ClassCastException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java,Eclipse和Android开发,所以我可能是简单的东西,虽然我已检查基本的东西,如拼写几次。

由于没有数字选择器控件(如日期选取器中使用)在公共Android SDK中的的传统智慧是存在于Android源克隆自己的控制。

在试图做到这一点,我抄了$ C $下NumberPicker到我的项目和它的依赖,NumberPickerButton。我也复制了配套资源。

我遇到的问题是,当我尝试包括在布局NumberPicker,在Eclipse的布局渲染的版本无法正常工作。

 < com.spencerandbrown.PatientTracker1.NumberPicker
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / spanEnd2/>
 

添加了标签导致XML文件的版式视图中只显示了以下错误。

  ClassCastException异常:com.spencerandbrown.PatientTracker1.NumberPickerButton不能转换为com.spencerandbrown.PatientTracker1.NumberPickerButton
 

我曾经尝试都完全合格的名称和不合格的名称和它没有什么区别。

但奇怪的是,控制工程在运行时,它只是将不会呈现在Eclipse。

有没有针对此问题的解决方案吗?我只是做错了什么?

如果有必要

,我可以包括更多code从我的项目,以澄清。

如果我引用内部Android部件,它工作正常,在Eclipse布局来看,却是笨拙的用这种方式进行交互,它可能会在未来突破。

 < com.android.internal.widget.NumberPicker
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / spanStart/>
 

下面是来自Eclipse中的堆栈跟踪。

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

解决方案

解决方法:我发现,如果我赶上并忽略ClassCastException异常,显然那就不要尝试用浏览我试图找到做任何事情,有没有进一步的问题,我可以再次使用布局编辑器:编辑器会相当愉快地加载,初始化并绘制有问题的观点,你就不能转换为同级车

 尝试{
    选择器=(NumberPicker)findViewById(R.id.myPicker);
    picker.blargh();
    //一切涉及选择器
}赶上(ClassCastException异常issue6894){} //忽略
 

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)

解决方案

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.

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

这篇关于努力扩大Android的XML布局当Eclipse ClassCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆