“android.view.View 无法转换为 android.view.ViewGroup”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11062761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:51:48  来源:igfitidea点击:

"android.view.View cannot be cast to android.view.ViewGroup" exception

androidandroid-layout

提问by Boda Taljo

I am working on an android application and I added a xml layout file that should be a fragment later:

我正在开发一个 android 应用程序,我添加了一个 xml 布局文件,它应该是一个片段:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imgTile"
        android:src="@drawable/tile_background"
        android:contentDescription="@string/CD_TILE_BACKGROUND_IMAGE"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ImageView>

</View>

When I try to move back to the graphical layout in Eclipse, I get this exception in the error log:

当我尝试移回 中的图形布局时Eclipse,我在错误日志中收到此异常:

java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:740)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:321)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:324)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1361)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1115)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:941)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegatePageChange(LayoutEditorDelegate.java:450)
    at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.pageChange(CommonXmlEditor.java:358)
    at org.eclipse.ui.part.MultiPageEditorPart.widgetSelected(MultiPageEditorPart.java:290)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4128)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1457)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1480)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1465)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1270)
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2746)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1433)
    at org.eclipse.swt.custom.CTabFolder.handleEvent(CTabFolder.java:257)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4128)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1457)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1480)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1465)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1270)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3974)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3613)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
    at org.eclipse.ui.internal.Workbench.access(Workbench.java:2499)
    at org.eclipse.ui.internal.Workbench.run(Workbench.java:679)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    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:344)
    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:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)

Can anyone help me with this exception?

任何人都可以帮我解决这个例外吗?

回答by Dheeresh Singh

I think you should use

我认为你应该使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imgTile"
        android:src="@drawable/tile_background"
        android:contentDescription="@string/CD_TILE_BACKGROUND_IMAGE"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ImageView>

</LinearLayout >

as View does not have any function like add(other_view) you can add a view in ViewGorup ....

因为 View 没有像 add(other_view) 这样的函数,你可以在 ViewGorup 中添加一个视图 ....

回答by yogesh prajapati

I know i am late to the party, but just in case some is banging his head for same problem, here is one possible solution that worked for me when i was trying to add zoom functionality in my pager adapter images. Info: this is not the exact answer, but a possible solution for those who are facing the same problem. I am posting this because i didn't fount this solution approach anywhere in stackOverflow

我知道我参加聚会迟到了,但以防万一有人因为同样的问题撞到他的头,这里有一个可能的解决方案,当我尝试在我的寻呼机适配器图像中添加缩放功能时,它对我有用。信息:这不是确切的答案,而是针对面临相同问题的人的可能解决方案。我发布这个是因为我没有在 stackOverflow 的任何地方找到这个解决方案

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/head_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/head_image" />

    </RelativeLayout>

In the above code, just Replace <ImageViewwith <com.yourPackage.TouchImageView.

在上面的代码中,只需替换<ImageView<com.yourPackage.TouchImageView.

TouchImageView(or any other class) is a custom class (you need to make)which should extend android.support.v7.widget.AppCompatImageView

TouchImageView(或任何其他类)是一个自定义类(您需要制作),它应该扩展android.support.v7.widget.AppCompatImageView