具有RecyclerView和CardView的片段错误:IllegalArgumentException:找不到片段ID的视图

时间:2018-09-08 06:57:41

标签: java android android-fragments android-recyclerview android-cardview

这是我没有碎片的成就。

Screenshot of Card view

现在我想使用片段来达到相同的结果。

这是我的MainActivity.java

public class MainActivity extends AppCompatActivity
implements Recycler_View_Fragment.OnFragmentInteractionListener,
    Row1.OnFragmentInteractionListener
{
    //Create the Fragment Manager
    FragmentManager fm;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //populate the fragment manager
    fm = getSupportFragmentManager();

    /*
      Step 4 Check that the Recycler_View_Fragment has not already been created.
      Otherwise create it
    */
    if(savedInstanceState == null)
    {
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.content, new Recycler_View_Fragment());
        transaction.commit();
    }
}

@Override
public void onFragmentInteraction(Uri uri)
{

}
}

这是我的activity_main.xml和its preview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/content"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

</LinearLayout>

这是Recycler_View_Fragment.java

public class Recycler_View_Fragment extends Fragment 
{

FragmentManager fm;

private OnFragmentInteractionListener mListener;

public Recycler_View_Fragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_recycler__view_, container, false);

    fm = getActivity().getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    transaction.replace(R.id.row_1_layout,new Row1());
    transaction.addToBackStack(null);
    transaction.commit();
    return view;
}
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

这是fragment_recycler_view_.xml和its preview

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".Recycler_View_Fragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" />

</RelativeLayout>

这是Row1.java

public class Row1 extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;
private ArrayList<String> mDataset;

private OnFragmentInteractionListener mListener;

public Row1() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    mDataset = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        mDataset.add("News " + i);
    }

    View view = inflater.inflate(R.layout.fragment_recycler__view_, container, false);
    mRecyclerView = view.findViewById(R.id.recyclerView);
    mAdapter = new Row1Adapter(mDataset);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
    return view;
}
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

public class Row1Adapter extends RecyclerView.Adapter<Row1Adapter.ViewHolder>
{
    private ArrayList<String> mDataset;

    public Row1Adapter(ArrayList<String> dataset)
    {
        this.mDataset = dataset;
    }

    @NonNull
    @Override
    public Row1Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View view;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_row1,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Row1Adapter.ViewHolder holder, int position)
    {
        holder.mTitle.setText(mDataset.get(position));
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView mTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.title);
        }
    }
}

}

这是fragment_row1.xml和its preview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/row_1_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="100dp"
    android:layout_margin="12dp"
    android:id="@+id/cardView"
    android:layout_height="200dp"
    android:background="#000">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

这是我得到的ERROR

我尝试了很多事情并搜索了google,但一无所获。

1 个答案:

答案 0 :(得分:0)

在Recycler_View_Fragment.java中,该行

transaction.replace(R.id.row_1_layout,new Row1());

正在尝试引用不包含在fragmen布局本身中的布局(使用id),即fragment_recycler__view_.xml。