Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用带有片段的RecyclerView_Java_Android_Android Fragments_Android 5.0 Lollipop_Android Recyclerview - Fatal编程技术网

Java 使用带有片段的RecyclerView

Java 使用带有片段的RecyclerView,java,android,android-fragments,android-5.0-lollipop,android-recyclerview,Java,Android,Android Fragments,Android 5.0 Lollipop,Android Recyclerview,我使用本教程中的CardView构建了RecycleView小部件: 现在我试着把它展示成一个片段 以下是我的零碎活动: public class FragmentActivity extends Fragment { private RecyclerView mRecyclerView; private CountryAdapter mAdapter; private LinearLayoutManager layoutManager; public Fr

我使用本教程中的CardView构建了RecycleView小部件:

现在我试着把它展示成一个片段

以下是我的零碎活动:

public class FragmentActivity extends Fragment {  
    private RecyclerView mRecyclerView;
    private CountryAdapter mAdapter;
    private LinearLayoutManager layoutManager;
    public FragmentActivity(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.card_layout, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
        layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
我称之为片段的主要活动: 公共类MainActivity扩展了活动{

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentActivity fragment = new FragmentActivity();
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .add(R.id.container_list, fragment).commit();
}
以下是我的活动单元主布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".CountryActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".CountryActivity"
        />
</RelativeLayout>

我的卡片布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="5dp"
    android:layout_height="match_parent">

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

        <ImageView
            android:id="@+id/countryImage"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:scaleType="centerCrop"
            android:tint="@color/photo_tint"
            android:layout_centerInParent="true"
            />

        <TextView
            android:id="@+id/countryName"
            android:gravity="center"
            android:background="?android:selectableItemBackground"
            android:focusable="true"
            android:clickable="true"
            android:layout_width="match_parent"
           android:layout_height="100dp"
           android:textSize="24sp"
           android:layout_centerInParent="true"
           android:textColor="@android:color/white"
           />

    </RelativeLayout>
</android.support.v7.widget.CardView>

和框架布局:

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

</FrameLayout>

应用程序在调用片段时崩溃。
感谢你们的帮助。

你们在
片段
类中夸大了错误的布局

View rootView = inflater.inflate(R.layout.card_layout, container, false);  //Here you are inflating the row layout instad of the fragment
只需将布局参考更改为正确的布局参考

View rootView = inflater.inflate(R.layout. activity_main, container, false);
此外,在MainActivity中,将布局资源更改为:

setContentView(R.layout.frame_layout);

请使用此代码,希望它能对您有所帮助。

您应该描述问题所在。您面临的意外行为是什么?调用碎片时应用程序崩溃。为什么您要使用名称“FragmentActivity”首先更改它,因为它已经是基类。这就是为什么getFragmentManager()什么都不提供。@kinsell你能提供应用程序崩溃时得到的日志吗?谢谢你的回答Carlos I修复了这个问题,但应用程序仍然崩溃。你得到的例外是什么?E/FragmentManager﹕ 找不到片段碎片活动{23833986#0 id=0x7f080042}的id 0x7f080042(com.empire.test01:id/container_list)的视图@Carlos J这是我得到的。我没有足够的声望点数,当我得到足够的声望点数时,我会确保你得到你的点数@Carlos JDidi你也可以更改你主要活动中的布局??请解释为什么在你发布答案时这样做有效,否则就没有那么有用了。@David你在创建Adapter对象之前设置了Adapter对象,当时它的get null obj导致应用程序崩溃的适配器ect。
public class DemoActivity extends Fragment {  
    private RecyclerView mRecyclerView;
    private CountryAdapter mAdapter;
    private LinearLayoutManager layoutManager;
    public DemoActivity(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.card_layout, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
        layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
        mRecyclerView.setAdapter(mAdapter);
        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }