在片段内的回收器视图中绑定视图

Binding views in a recycler view inside a fragment

所以我试图让一个菜单按钮在我的回收站视图之上工作,但是我收到了错误

02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime: java.lang.RuntimeException: Unable to bind views for lucas.app_2001.OneFragment

我已经想尽办法了,但我不知道如何解决。 这是代码:

package lucas.app_2001;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import com.michaldrabik.tapbarmenulib.TapBarMenu;
import java.lang.*;
import java.util.ArrayList;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class OneFragment extends Fragment  {
    static ArrayList<Person> persons = new ArrayList<>();
    private  RecyclerView.LayoutManager rv;
    private RecyclerView.Adapter adapter;
    ImageButton rate;
    Button NewShout;
    @Bind(R.id.tapBarMenu)TapBarMenu tapBarMenu;


    public OneFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyclerview_activity, container, false);
        final View rootViews = inflater.inflate(R.layout.cardview_activity, container, false);
        final FragmentActivity c = getActivity();
        RecyclerView recyclev = (RecyclerView) view.findViewById(R.id.rv);
        rv = new LinearLayoutManager(getActivity());
        OneFragment.initializeData();
        adapter = new RVAdapter(persons);
        recyclev.setLayoutManager(rv);
        recyclev.setHasFixedSize(true);
        recyclev.setAdapter(adapter);
        ButterKnife.bind(this, view);
        rate = (ImageButton) view.findViewById(R.id.rate);

        return view;
    }


    @OnClick(R.id.tapBarMenu) public void onMenuButtonClick() {
        tapBarMenu.open();
    }

    @OnClick({ R.id.item1, R.id.item2}) public void onMenuItemClick(View view) {
        tapBarMenu.close();
        switch (view.getId()) {
            case R.id.item1:
                Log.i("TAG", "Item 1 selected");
                break;
            case R.id.item2:
                Log.i("TAG", "Item 2 selected");
                break;
        }
    }

    public void updateDetail() {
        Intent intent = new Intent(getActivity(), Shout.class);
        startActivity(intent);

    }


    public static void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.icorating0, R.drawable.icorating0, "75pts."));
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.icorating0, R.drawable.icorating0, "20pts."));
        persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.icorating0, R.drawable.icorating0, "30pts."));
        persons.add(new Person("Lillie Watts", "35 years old", R.drawable.icorating0, R.drawable.icorating0, "40pts."));
    }

}

这是回收站视图activity:

  package lucas.app_2001;

import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

import butterknife.ButterKnife;


public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

    public View onCreate(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.recyclerview_activity, container, false);
        ButterKnife.bind(this, rootView);

        return rootView;
    }
    private List<Person> persons;


    public class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        ImageButton StarRate;

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.recyclerview_activity, container, false);
            ButterKnife.bind(this, rootView);

            return rootView;

        }
        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            personName = (TextView) itemView.findViewById(R.id.person_name);
            personAge = (TextView) itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
            StarRate = (ImageButton) itemView.findViewById(R.id.rate);


            StarRate.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {

                    getAdapterPosition();
                    if (getAdapterPosition() == 1) {
                        Toast.makeText(v.getContext(), "Position" + getAdapterPosition(), Toast.LENGTH_LONG).show();
//                        Intent activityChangeIntent = new Intent(lucas.app_2001.RVAdapter.this, MapsActivity.class);


                    }
                    if (getAdapterPosition() == 2) {
                        Toast.makeText(v.getContext(), "Position" + getAdapterPosition(), Toast.LENGTH_LONG).show();
//                        Intent activityChangeIntent = new Intent(lucas.app_2001.RVAdapter.this, MapsActivity.class);


                    }
                    if (getAdapterPosition() == 3) {
                        Toast.makeText(v.getContext(), "Position" + getAdapterPosition(), Toast.LENGTH_LONG).show();
//                        Intent activityChangeIntent = new Intent(lucas.app_2001.RVAdapter.this, MapsActivity.class);


                    }
                    if (getAdapterPosition() == 4) {
                        Toast.makeText(v.getContext(), "Position" + getAdapterPosition(), Toast.LENGTH_LONG).show();
//                        Intent activityChangeIntent = new Intent(lucas.app_2001.RVAdapter.this, MapsActivity.class);


                    }   if (getAdapterPosition() == 5) {
                        Toast.makeText(v.getContext(), "Position" + getAdapterPosition(), Toast.LENGTH_LONG).show();
//                        Intent activityChangeIntent = new Intent(lucas.app_2001.RVAdapter.this, MapsActivity.class);


                    }
                    if (getAdapterPosition() == 6) {
                        Toast.makeText(v.getContext(), "Position" + getAdapterPosition(), Toast.LENGTH_LONG).show();
//                        Intent activityChangeIntent = new Intent(lucas.app_2001.RVAdapter.this, MapsActivity.class);
                    }


                }
            });

        }
    }


    public RVAdapter(List<Person> persons) {

        this.persons = persons;
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

    }
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup,  int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);


        return pvh;
    }


    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {

        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
        personViewHolder.StarRate.setImageResource(persons.get(i).rate);

    }



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

}

RecycleViewxml 文件:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/Recycle"
    android:weightSum="1">


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



        <android.support.v7.widget.RecyclerView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/rv"
            android:layout_weight="11.17">

        </android.support.v7.widget.RecyclerView>

        <com.michaldrabik.tapbarmenulib.TapBarMenu
            android:id="@+id/tapBarMenu"
            android:layout_width="57dp"
            android:layout_height="450dp"
            xmlns:android="http://schemas.android.com/apk/res/android"
            app:tbm_buttonPosition= "left"
            app:tbm_backgroundColor = "@color/wallet_holo_blue_light"
            android:layout_gravity="center|right"/>

    </RelativeLayout>


</RelativeLayout>

我设法让它在没有回收站视图的片段上工作,但是当添加回收站视图时,出于某种原因出现了问题,欢迎任何反馈

这是完整的错误:

02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime: FATAL EXCEPTION: main
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime: Process: lucas.app_2001, PID: 8212
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime: java.lang.RuntimeException: Unable to bind views for lucas.app_2001.OneFragment
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at butterknife.ButterKnife.bind(ButterKnife.java:322)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at butterknife.ButterKnife.bind(ButterKnife.java:279)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at lucas.app_2001.OneFragment.onCreateView(OneFragment.java:49)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1450)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.View.measure(View.java:16521)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:544)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5146)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
02-01 17:33:53.946 8212-8212/lucas.app_2001 E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Required view 'item1' with ID 2131624127 for method 'onMenuItemClick' was not found. If this view 

R.id.item1, R.id.item2 不见了。

检查这些..

您需要将这些项目添加到..

<TapBarMenu ..> .. </TapBarMenu>