Android fragments 无法将android.widget.ImageView强制转换为android.view.ViewGroup
在Android应用程序中,单击菜单按钮后,MainActivity应该用WebView片段替换ImageView片段。相反,它抛出: java.lang.ClassCastException:android.widget.ImageView不能强制转换为android.view.ViewGroup 在Eclipse中,我清理了项目,删除了R文件,按照其他线程中的建议检查了XML文件,但没有成功 如果你能帮忙,我将不胜感激Android fragments 无法将android.widget.ImageView强制转换为android.view.ViewGroup,android-fragments,android-webview,android-imageview,android-fragmentactivity,classcastexception,Android Fragments,Android Webview,Android Imageview,Android Fragmentactivity,Classcastexception,在Android应用程序中,单击菜单按钮后,MainActivity应该用WebView片段替换ImageView片段。相反,它抛出: java.lang.ClassCastException:android.widget.ImageView不能强制转换为android.view.ViewGroup 在Eclipse中,我清理了项目,删除了R文件,按照其他线程中的建议检查了XML文件,但没有成功 如果你能帮忙,我将不胜感激 public class MainActivity extends Fr
public class MainActivity extends FragmentActivity implements PhotoFragment.Callbacks {
[……]
fragment_photo.xml
<ImageView
android:id="@+id/photoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:scaleType="fitXY" />
web_view.xml
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:scaleType="fitXY" />
我找到了问题所在 我试图传递用于构建片段的ImageView(R.id.photoView)的资源id,而不是实际片段。由于此片段不是从布局创建的,因此我需要使用其对象引用并传递其ContainerWebID:
// fragment previously created in MainActivity:
PhotoFragment f = PhotoFragment.newInstance();
因此,为了解决我所替代的问题:
fragmentTransaction.replace(R.id.photoView, web_f);
与:
fragmentTransaction.replace(R.id.photoView, web_f);
fragmentTransaction.replace(((ViewGroup)f.getView().getParent()).getId() , web_f);