当我在聊天应用程序菜单上单击“查找朋友”时,我的代码导致我的应用程序崩溃,

时间:2019-09-15 18:46:27

标签: java android android-recyclerview

此代码导致错误。我想适配器何时开始侦听。

package com.example.wherechat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
//import com.google.firebase.database.Query;
import com.google.firebase.database.Query;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class FindFriendsActivity extends AppCompatActivity
{
    private Toolbar mToolBar;
    private RecyclerView FindFriendRecyclerList;
    private DatabaseReference UsersRef;


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

        UsersRef= FirebaseDatabase.getInstance().getReference().child("Users");
        FindFriendRecyclerList=(RecyclerView)findViewById(R.id.find_friends_recycler_list);
        FindFriendRecyclerList.setLayoutManager(new LinearLayoutManager(this));

        mToolBar=(Toolbar)findViewById(R.id.find_friends_toolbar);

        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Find Friends");
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        Query query = FirebaseDatabase
                .getInstance()
                .getReference().child("Users");

        FirebaseRecyclerOptions<contacts> options= new FirebaseRecyclerOptions.Builder<contacts>().
                setQuery(UsersRef,contacts.class)
                .build();

        FirebaseRecyclerAdapter<contacts,FindFriendsViewHolder>adapter=
                new FirebaseRecyclerAdapter<contacts,FindFriendsViewHolder>(options){
            @Override
            protected void onBindViewHolder(@NonNull FindFriendsViewHolder findFriendsViewHolder, int i, @NonNull contacts contacts)
            {
                findFriendsViewHolder.userName.setText((contacts.getName()));
                findFriendsViewHolder.userStatus.setText(contacts.getStatus());
                Picasso.get().load(contacts.getImage()).placeholder(R.drawable.profile_image).into(findFriendsViewHolder.profileImage);
             }

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

        FindFriendRecyclerList.setAdapter(adapter);
        adapter.startListening();

    }

    public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
    {
        TextView userName,userStatus;
        CircleImageView profileImage;

        public FindFriendsViewHolder(@NonNull View itemView)
        {
            super(itemView);
            userName=itemView.findViewById(R.id.user_profile_name);
            userStatus=itemView.findViewById(R.id.user_online_status);
            profileImage=itemView.findViewById(R.id.users_profile_image);

        }
    }
}

这是我收到的logcat错误。

2019-09-15 22:21:51.483 17524-17524/com.example.wherechat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wherechat, PID: 17524
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView
    at com.example.wherechat.FindFriendsActivity$FindFriendsViewHolder.<init>(FindFriendsActivity.java:97)
    at com.example.wherechat.FindFriendsActivity$1.onCreateViewHolder(FindFriendsActivity.java:77)
    at com.example.wherechat.FindFriendsActivity$1.onCreateViewHolder(FindFriendsActivity.java:63)
    at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
    at android.view.View.layout(View.java:19590)
    at android.view.ViewGroup.layout(ViewGroup.java:6053)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)

我使用的依赖项是否有问题?我正在使用16.0版。 我的目的是从数据库中选择并检索用户数量,然后在我的聊天应用程序中单击“查找朋友”后,使用recyclerView显示用户。

  apply plugin: 'com.android.application'
  apply plugin: 'com.google.gms.google-services'

   android {
   compileSdkVersion 29
   buildToolsVersion "29.0.1"
   defaultConfig {
    applicationId "com.example.wherechat"
    minSdkVersion 19
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   }
    buildTypes {
      release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android- 
      optimize.txt'), 'proguard-rules.pro'
       }
   }
}
  dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'androidx.appcompat:appcompat:1.0.2'
   implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
   implementation 'com.google.firebase:firebase-auth:16.0.5'
   implementation 'de.hdodenhof:circleimageview:3.0.1'
   implementation 'com.google.firebase:firebase-database:16.0.4'
   implementation 'com.google.firebase:firebase-storage:16.0.4'
   implementation 'androidx.legacy:legacy-support-v4:1.0.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'androidx.test:runner:1.2.0'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
   implementation 'com.google.android.material:material:1.0.0-rc01'
   implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.+'

   //Use if below implementations not working.
   /*implementation 'com.google.firebase:firebase-auth:11.8.0'
   implementation 'com.google.firebase:firebase-database:11.8.0'
   implementation 'com.google.firebase:firebase-storage:11.8.0'
   implementation 'com.google.firebase:firebase-core:11.8.0'
   16.0.1*/

    implementation 'com.google.firebase:firebase-core:16.0.4'
   //implementation 'com.google.firebase:firebase-auth:16.0.5'
   // implementation 'com.google.firebase:firebase-database:16.0.3'
  //implementation 'com.google.firebase:firebase-storage:16.0.3'

   implementation 'com.squareup.picasso:picasso:2.71828'

  /*implementation 'com.google.firebase:firebase-auth:11.0.4 '
  implementation 'com.google.firebase:firebase-database:11.0.4 '
  implementation 'com.google.firebase:firebase-storage:11.0.4 '*/

  implementation 'com.firebaseui:firebase-ui-database:4.1.0'
  //implementation 'com.firebaseui:firebase-ui-database:6.0.0'

 }

我的应用程序用户界面:

      https://i.stack.imgur.com/J7X9T.png

这是我的XML文件。即使我将userName和userStatus作为TextView,它也会显示转换错误。

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">


 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:orientation="horizontal">


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/users_profile_image"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:src="@drawable/profile_image"/>


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


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

           <TextView
               android:id="@+id/user_profile_name"
               android:layout_width="200dp"
               android:layout_height="wrap_content"
               android:text="user name"
               android:textAlignment="textStart"
               android:textSize="18sp"
               android:textStyle="bold"
               />

           <ImageView
               android:id="@+id/user_online_status"
               android:layout_width="18dp"
               android:layout_height="18dp"
               android:layout_marginTop="5dp"
               android:src="@drawable/online"
               android:visibility="invisible"/>
       </LinearLayout>


       <TextView
           android:id="@+id/user_status"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="user status"
           android:textAlignment="textStart"
           android:textSize="16sp"
          />

      </LinearLayout>
   </LinearLayout>
 </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

主要原因在这里

  

java.lang.ClassCastException:   androidx.appcompat.widget.AppCompatImageView无法转换为   android.widget.TextView

这意味着您正在将ImageView投射到TextView。那么问题是什么:xml中的问题。问题是userNameuserStatus的ID被交换了。检查您的 xml

更新后的答案

看你在误会。在xml中,您在ImageView中使用 user_online_status id,但在FindFriendsViewHolder中,您正在使用userStatus=itemView.findViewById(R.id.user_online_status);并看到您声明 userStatus 为textView,但它将是imageView

答案 1 :(得分:0)

在您的xml上,您将定义的userName或userStatus定义为Image View,而您的代码是TextView。