简体   繁体   English

无法在Android中将v4.FragmentActivity转换为v4.Fragment?

[英]Cannot cast v4.FragmentActivity to v4.Fragment in android?

I am using android.support.v4.app for Fragment , FragmentTabHost and Fragment . 我正在为FragmentFragmentTabHostFragment使用android.support.v4.app My goal is to show two tabs inside a fragment itself. 我的目标是在片段本身中显示两个选项卡。 So I followed the following process: 因此,我遵循以下过程:

ListingFragment ListingFragment

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 ListingFrgmentTabHost

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

whereas FragmentActivity is import android.support.v4.app.FragmentActivity; FragmentActivityimport android.support.v4.app.FragmentActivity;

fragment_listing_home 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>

But when I am running the above snippets. 但是,当我运行上述片段时。 It's throwing me cast exception ie java.lang.ClassCastException: com.app.grabhouse.fragment.ListingFragmentTabHost cannot be cast to android.support.v4.app.Fragment 它扔我cast的例外即java.lang.ClassCastException: com.app.grabhouse.fragment.ListingFragmentTabHost cannot be cast to android.support.v4.app.Fragment

Why is this happening? 为什么会这样呢?

Edit 编辑

After the suggestion I have converted my FragmentActivity to 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);
}
}

因为您需要将一个片段传递到addTabListingFragmentTabHost不是一个片段,所以它是一个活动

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将ContraceptiveFragment中的ContraceptiveFragment()应用于(android.support.v4.app.FragmentActivity) - ContraceptiveFragment( ) in ContraceptiveFragment cannot be applied to (android.support.v4.app.FragmentActivity) 从 v4.Fragment 请求运行时权限并让回调转到 Fragment? - Request runtime permissions from v4.Fragment and have callback go to Fragment? 无法在ViewModelProviders中解析android.support.v4.app.FragmentActivity的方法 - Cannot resolve method of android.support.v4.app.FragmentActivity in ViewModelProviders android.support.v4.app.fragment和androidx.fragment.app.FragmentActivity有什么区别 - What is the difference between android.support.v4.app.fragment and androidx.fragment.app.FragmentActivity com.example.main无法转换为android.support.v4.app.Fragment - com.example.main cannot be cast to android.support.v4.app.Fragment 不可转换的类型; 无法将“android.support.v4.app.Fragment”转换为“com.google.android.gms.maps.SupportMapFragment” - Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment' CalendarFragment无法转换为android.support.v4.app.LoaderManager $ LoaderCallbacks - CalendarFragment cannot be cast to android.support.v4.app.LoaderManager$LoaderCallbacks android.support.v4.widget.DrawerLayout无法转换为android.support.v4.widget.DrawerLayout $ DrawerListener - android.support.v4.widget.DrawerLayout cannot be cast to android.support.v4.widget.DrawerLayout$DrawerListener RelativeLayout无法强制转换为android.support.v7.widget.Toolbar - RelativeLayout cannot be cast to android.support.v7.widget.Toolbar CoordinatorLayout 无法转换为 android.support.v7.widget.Toolbar - CoordinatorLayout cannot be cast to android.support.v7.widget.Toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM