java - 在这里,我试图通过适配器从 firebase 实时数据库中获取用户,但我被困在这里
问题描述
在我的手机上运行该应用程序后,搜索片段崩溃并显示一些反射Util:没有此类方法异常。另外,我已经在 Gradle 中实现了所有依赖项。我被困在这里 4 小时,请帮帮我... compileSdkVersion 29 minSdkVersion 21 targetSdkVersion 29 MY UserAdapter CODE
package com.example.ayushapk.postbook.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.ayushapk.postbook.Model.User;
import com.example.ayushapk.postbook.R;
import com.google.firebase.auth.FacebookAuthCredential;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context context;
private List<User> userList;
private boolean isFragment;
public UserAdapter(Context context, List<User> userList, boolean isFragment) {
this.context = context;
this.userList = userList;
this.isFragment = isFragment;
}
private FirebaseUser firebaseUser;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(context).inflate(R.layout.user_item, parent, false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final User user = userList.get(position);
holder.btn_follow.setVisibility(View.VISIBLE);
holder.Username.setText(user.getUsername());
holder.Name.setText(user.getName());
Picasso.get().load(user.getImageurl()).placeholder(R.drawable.avatar).into(holder.profileImage);
isFollowed(user.getId(), holder.btn_follow);
if(user.getId().equals(firebaseUser.getUid()))
holder.btn_follow.setVisibility(View.GONE);
}
private void isFollowed(final String id, final Button btn_follow) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Follow")
.child(firebaseUser.getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child(id).exists()){
btn_follow.setText(R.string.following);
}else{
btn_follow.setText(R.string.follow);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public int getItemCount() {
return userList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView Username, Name;
private Button btn_follow;
private CircleImageView profileImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
Username = itemView.findViewById(R.id.username);
Name = itemView.findViewById(R.id.name);
btn_follow = itemView.findViewById(R.id.btn_follow);
profileImage = itemView.findViewById(R.id.profile_image_search);
}
}
}
user_item 布局
<?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="wrap_content"
android:padding="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image_search"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/avatar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/profile_image_search"
android:orientation="vertical">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:textColor="@color/design_default_color_primary_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/full_name" />
</LinearLayout>
<Button
android:id="@+id/btn_follow"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="@drawable/button_background"
android:textColor="@color/colorPrimary"
android:visibility="gone" />
</RelativeLayout>
搜索片段
package com.example.ayushapk.postbook.fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.ayushapk.postbook.Adapter.UserAdapter;
import com.example.ayushapk.postbook.Model.User;
import com.example.ayushapk.postbook.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.hendraanggrian.appcompat.widget.SocialAutoCompleteTextView;
import java.util.ArrayList;
import java.util.List;
public class SearchFragment extends Fragment {
private RecyclerView recyclerViewUsers;
private List<User> userList;
private UserAdapter userAdapter;
private SocialAutoCompleteTextView searchBar;
private DatabaseReference reference;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_search, container, false);
searchBar = mView.findViewById(R.id.search_bar);
recyclerViewUsers = mView.findViewById(R.id.recycler_view_users);
recyclerViewUsers.setHasFixedSize(true);
recyclerViewUsers.setLayoutManager(new LinearLayoutManager(getActivity()));
userList = new ArrayList<>();
userAdapter = new UserAdapter(getActivity(), userList, true);
recyclerViewUsers.setAdapter(userAdapter);
showUsers();
return mView;
}
private void showUsers() {
reference = FirebaseDatabase.getInstance().getReference().child("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(TextUtils.isEmpty(searchBar.getText().toString())){
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
userList.add(user);
}
userAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), "something Error " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
search_fragment 布局
<?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"
tools:context=".fragments.SearchFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/search_icon"
android:src="@drawable/ic_search" />
<com.hendraanggrian.appcompat.widget.SocialAutoCompleteTextView
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:background="?android:attr/windowBackground"
android:hint="@string/search" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_users"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/app_bar_search"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_tags"
android:layout_below="@+id/recycler_view_users"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
错误
06-12 16:19:03.643 22251-22251/? W/ReflectionUtils: java.lang.NoSuchMethodException: android.os.MessageQueue#enableMonitor()#bestmatch
at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:338)
at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:375)
at miui.util.ReflectionUtils.callMethod(ReflectionUtils.java:800)
at miui.util.ReflectionUtils.tryCallMethod(ReflectionUtils.java:818)
at android.os.BaseLooper.enableMonitor(BaseLooper.java:47)
at android.os.Looper.prepareMainLooper(Looper.java:111)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
06-12 16:19:03.695 22251-22251/com.example.ayushapk.postbook W/ResourceType: No package identifier when getting name for resource number 0x00000000
06-12 16:19:03.706 22251-22251/com.example.ayushapk.postbook W/System: ClassLoader referenced unknown path: /data/app/com.example.ayushapk.postbook-2/lib/arm64
06-12 16:19:03.759 22251-22277/com.example.ayushapk.postbook W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
06-12 16:19:03.773 22251-22251/com.example.ayushapk.postbook I/FirebaseInitProvider: FirebaseApp initialization successful
06-12 16:19:03.790 22251-22251/com.example.ayushapk.postbook W/ResourceType: No package identifier when getting name for resource number 0x00000000
06-12 16:19:03.800 22251-22280/com.example.ayushapk.postbook W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
06-12 16:19:03.800 22251-22251/com.example.ayushapk.postbook W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
应用级构建 Gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.ayushapk.postbook"
minSdkVersion 21
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.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.firebase:firebase-database:19.3.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.google.firebase:firebase-functions:19.0.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "com.hendraanggrian.appcompat:socialview:0.2"
// auto-complete hashtag and mention
implementation "com.hendraanggrian.appcompat:socialview-commons:0.2"
api 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
解决方案
推荐阅读
- laravel - Laravel Valet 拒绝连接一个特定项目
- c# - 为 JSON 序列化选择要包含/排除的类属性
- xamarin - 删除 XamarinForms 中的 ActionBar 顶部标题
- python - CNN 架构中的神经元数量
- pandas - Pandas - numpy 和多个条件
- angular - 断言错误:此 TNode 不属于此 TView - Angular 从 v8 更新到 v10 之后
- r - 与 R Postgres 后端中唯一/不同的类似聚合函数
- django - 问题将 id 传递到 url 尝试在 Django 中获取
- angular - 如何减少对象分配中的重复代码
- reactjs - 使用 React 17.0.01 的后端 Spring Boot RESTful Web 服务不起作用 tomcat 8.4.25