我正在通过Android ApiDemos中的示例程序FragmentArguments研究Fragment。
布局文件“fragment_arguments.xml”用于此示例,如下所示。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="4dip"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="top|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/fragment_arguments_msg" />
<LinearLayout android:orientation="horizontal" android:padding="4dip"
android:layout_width="match_parent" android:layout_height="wrap_content">
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
android:id="@+id/embedded"
android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="1"
android:label="@string/fragment_arguments_embedded" />
<FrameLayout
android:id="@+id/created"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
以下代码段。
public class FragmentArguments extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_arguments);
if (savedInstanceState == null) {
// First-time init; create fragment to embed in activity.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment newFragment = MyFragment.newInstance("From Arguments");
ft.add(R.id.created, newFragment);
ft.commit();
}
}
我试图像下面那样切换fragment和FrameLayout标签的顺序而不修改java文件中的任何代码。
<FrameLayout
android:id="@+id/created"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1" />
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
android:id="@+id/embedded"
android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="1"
android:label="@string/fragment_arguments_embedded" />
但是我得到了ClassCastException,如下所示。
>04-11 15:36:06.653: E/AndroidRuntime(27020): FATAL EXCEPTION: main
04-11 15:36:06.653: E/AndroidRuntime(27020): Process: com.example.android.apis, PID: 27020
04-11 15:36:06.653: E/AndroidRuntime(27020): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.apis/com.example.android.apis.app.FragmentArguments}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.ActivityThread.access$800(ActivityThread.java:156)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.os.Looper.loop(Looper.java:157)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.ActivityThread.main(ActivityThread.java:5872)
04-11 15:36:06.653: E/AndroidRuntime(27020): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 15:36:06.653: E/AndroidRuntime(27020): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 15:36:06.653: E/AndroidRuntime(27020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-11 15:36:06.653: E/AndroidRuntime(27020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
04-11 15:36:06.653: E/AndroidRuntime(27020): at dalvik.system.NativeStart.main(Native Method)
04-11 15:36:06.653: E/AndroidRuntime(27020): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1106)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.BackStackRecord.run(BackStackRecord.java:690)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1571)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.Activity.performStart(Activity.java:5321)
04-11 15:36:06.653: E/AndroidRuntime(27020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2569)
04-11 15:36:06.653: E/AndroidRuntime(27020): ... 11 more
我试图从FragmentManager.java的源代码中找出根本原因 它应该在这一行崩溃:
container = (ViewGroup)mContainer.findViewById(f.mContainerId);
但是在使用调试器确保f.mContainerId等于R.id.created(即FrameLayout)并且它应该是ViewGroup之后,它仍然让我感到困惑。
以前有人遇到过这个问题吗?任何评论都表示赞赏。