我能够从Firebase检索数据,但不能显示在RecyclerViewCardView中。我在这里发布代码。我想在RecyclerView中显示电话号码列表。当我单击该按钮以显示电话号码列表时,它不显示任何内容,并且应用程序停止运行。请帮我。

先谢谢了。在logcat中发生以下异常:

10-09 17:52:37.542 20803-20803/com.jk.login E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jk.login, PID: 20803
    android.view.InflateException: Binary XML file line #21: Error inflating class de.hdodenhof.circleimageview.CircleImageView
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at com.jk.login.MembersAdaptor.onCreateViewHolder(MembersAdaptor.java:38)
        at com.jk.login.MembersAdaptor.onCreateViewHolder(MembersAdaptor.java:19)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15779)
        at android.view.ViewGroup.layout(ViewGroup.java:5069)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2166)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1916)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1117)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6184)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:773)
        at android.view.Choreographer.doCallbacks(Choreographer.java:586)
        at android.view.Choreographer.doFrame(Choreographer.java:556)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:759)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5422)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.


MyCircle.java

public class MyCircle extends AppCompatActivity {
    RecyclerView recyclerView;
     RecyclerView.Adapter adapter
    RecyclerView.LayoutManager layoutManager;
    FirebaseAuth mAuth;
    FirebaseUser user;
    ArrayList<Phone> nameList;
    String circlememberid;
    DatabaseReference userReference;
    Phone phone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_my_circle );
        recyclerView = findViewById( R.id.recyclerView );
        mAuth = FirebaseAuth.getInstance();
        user = mAuth.getCurrentUser();
        nameList = new ArrayList<>( );


        layoutManager = new LinearLayoutManager( this );
        recyclerView.setHasFixedSize( true );
        recyclerView.setLayoutManager( layoutManager );

        userReference = FirebaseDatabase.getInstance().getReference().child("Users");
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child( user.getPhoneNumber() ).child( "CircleMembers" );

        reference.addValueEventListener( new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                nameList.clear();
                if(dataSnapshot.exists()) {
                    for(DataSnapshot  dss:dataSnapshot.getChildren()) {
                        circlememberid = dss.child( "circlememberid" ).getValue(String.class);
                        userReference.child( circlememberid ).addListenerForSingleValueEvent( new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                phone = dataSnapshot.getValue( Phone.class );
                                nameList.add( phone );
                                adapter.notifyDataSetChanged();

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        } );

                        adapter = new MembersAdaptor(nameList,getApplicationContext());
                        recyclerView.setAdapter( adapter );
                        adapter.notifyDataSetChanged();


                    }
                 }

    }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        } );


MembersAdapter.java

public class MembersAdaptor extends RecyclerView.Adapter<MembersAdaptor.MemberViewHolder>{
    ArrayList<Phone> nameList;
    Context context;

    MembersAdaptor(ArrayList<Phone> nameList, Context context){
        this.nameList = nameList;
        this.context = context;
    }

    @Override
    public int getItemCount() {

        return nameList.size();
    }

    @NonNull
    @Override
    public MemberViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.card_layout,viewGroup,false );

        MemberViewHolder memberViewHolder = new MemberViewHolder(view,context,nameList );
        return memberViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MemberViewHolder memberViewHolder, int i) {
        Phone currentUserObj = nameList.get( i );
        memberViewHolder.txtName.setText( currentUserObj.phonenumber);
   }

    public static class MemberViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView txtName;
        View view;
        Context context;
        ArrayList<Phone> nameArrayList;
        FirebaseAuth mAuth;
        FirebaseUser user;

        public MemberViewHolder(@NonNull View itemView, Context context, ArrayList<Phone> nameArrayList) {
            super( itemView );

            this.context = context;
            this.nameArrayList = nameArrayList;
            itemView.setOnClickListener( this );
            mAuth = FirebaseAuth.getInstance();
            user = mAuth.getCurrentUser();

            txtName = itemView.findViewById( R.id.item_title );  //want to show circlememberid in this this textview in cardlayout

        public  void onClick(View v){

            Toast.makeText(context,"You have clicked this user",Toast.LENGTH_LONG ).show();
        }
    }
}
}
}


电话.java

public class Phone {
public  String phonenumber;

    public Phone(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public Phone(){

    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }
}


card_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:cardview="http://schemas.android.com/tools"
    android:layout_margin="6dp"
    cardview:cardElevation="3dp"
    cardview:cardCornerRadius="12dp"
    cardview:contentPadding="4dp"
    android:foreground="?selectableItemBackground"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="14dp">

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="10"
        android:orientation="horizontal">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/circleImageView1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:src="@drawable/defaultpicture" />
        <TextView
            android:id="@+id/item_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="7"
            android:textStyle="bold"
            android:textSize="22sp"
            android:text="My User"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:src="@drawable/red_offline"/>
    </LinearLayout>

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

最佳答案

我不知道该CircleImageView类的工作原理,但是正如我在您的代码中看到的那样,这给您带来麻烦。我能想到的最简单的方法是在Android上使用Glide library

因此,不用使用该自定义CircleImageView,只需使用ImageView,添加以下依赖项即可:

implementation 'com.github.bumptech.glide:glide:4.7.1'


在您的活动中找到图像视图,并使用以下代码行设置图像:

Glide.with(context)
    .load(yourImage)
    .apply(RequestOptions.circleCropTransform())
    .into(imageView);


而已!

10-07 18:05