【问题标题】:Cannot cast v4.FragmentActivity to v4.Fragment in android?无法在 android 中将 v4.FragmentActivity 转换为 v4.Fragment?
【发布时间】:2016-06-25 23:14:44
【问题描述】:

我将android.support.v4.app 用于FragmentFragmentTabHostFragment。我的目标是在片段本身内显示两个选项卡。所以我遵循了以下流程:

列表片段

public class ListingFragment extends Fragment {
private FragmentTabHost mTabHost;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_listing_home,container, false);
  mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabHost);

  mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
  mTabHost.addTab(mTabHost.newTabSpec("listfragment").setIndicator("Fragment List"), ListingFragmentTabHost.class, null);
  return rootView;
}

ListingFrgmentTabHost

public class ListingFragmentTabHost extends FragmentActivity {
   ....
   ....
}

FragmentActivityimport android.support.v4.app.FragmentActivity;

fragment_listing_home

<android.support.v4.app.FragmentTabHost
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/tabHost"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

但是当我运行上面的 sn-ps 时。它抛出了我cast 异常,即java.lang.ClassCastException: com.app.grabhouse.fragment.ListingFragmentTabHost cannot be cast to android.support.v4.app.Fragment

为什么会这样?

编辑

根据建议,我已将我的 FragmentActivity 转换为 Fragment

public class ListingFragmentTabHost extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_listing_home, container, false);
    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    recyclerView = (RecyclerView) getView().findViewById(R.id.mRecyclerView);
    super.onActivityCreated(savedInstanceState);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(llm);

    HomeRecyclerViewAdapter homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getActivity().getApplicationContext());
    recyclerView.setAdapter(homeRecyclerViewAdapter);
}
}

【问题讨论】:

    标签: java android android-fragments android-fragmentactivity


    【解决方案1】:

    因为你需要将一个片段传递给addTab,而ListingFragmentTabHost不是一个片段,它是一个活动

    【讨论】:

    • 也就是说这个问题的第一个答案是错误的? stackoverflow.com/questions/20469877/…
    • 啊!我的错。请查看以上评论
    • 如果你说的是一个有 52 个赞成票的人,不正确,不同之处在于他使用的是所有由活动托管的嵌套片段
    • 感谢您指出这一点。现在我使用Fragment 而不是FragmentActivity 但现在它显示No tab known for tag null。我用谷歌搜索并知道原因并在OnCreateView 中定义了所有内容,但仍然抛出相同的错误。
    • 请查看相关的 Edit-1 部分
    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多