如何解决自定义适配器中空对象引用上的 ImageView setOnClickListener
我从互联网上拿了这个适配器,但遇到了问题:
Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'void android.widget.ImageView.setonClickListener(android.view.View$OnClickListener)' on a null object reference
.Consts.CustomFriendsRequets.getView
这里 CustomFriendRequest 是我的适配器,它的副本工作正常,但这个不想要。错误是指第 104 行,即 viewHolder.add.setonClickListener (v ->
。我花了几天时间浏览代码,试图找出问题所在。
这是完整的适配器代码。自定义好友请求:
class CustomFriendsRequets extends ArrayAdapter<addFriends> implements View.OnClickListener{
private ArrayList<addFriends> dataSet;
private String uid,uid2;
private DatabaseReference isRef;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
TextView txtType;
ImageView info;
ImageView add;
ImageView del;
}
public CustomFriendsRequets(ArrayList<addFriends> data,Context context) {
super(context,R.layout.friends_requests_item,data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
addFriends dataModel=(addFriends) object;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
// Get the data item for this position
addFriends dataModel = getItem(position);
// Check if an existing view is being reused,otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.friends_item,parent,false);
viewHolder.txtName = convertView.findViewById(R.id.tvDescr);
viewHolder.txtType = convertView.findViewById(R.id.tvPrice);
viewHolder.info = convertView.findViewById(R.id.uImgreqs);
viewHolder.add = convertView.findViewById(R.id.ibAdd);
viewHolder.del = convertView.findViewById(R.id.ibDel);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();if(user!=null){uid=user.getUid();}
uid2 = dataModel.uid2;
viewHolder.txtName.setText(dataModel.username1);
viewHolder.txtType.setText("хочет добавить вас в друзья");
viewHolder.info.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(),EnemyProfile.class);
intent.putExtra(Constant.UID,dataModel.uid2);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
}
});
isRef = FirebaseDatabase.getInstance().getReference();
viewHolder.add.setonClickListener(v -> //error here
isRef.child("Users").child("info").child(uid2).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Users info = snapshot.getValue(Users.class);
if (info!=null){
String name = info.username;
String id = info.uid;
addFriends adf = new addFriends(id,name);
isRef.child("Users").child(uid).child("Friends").setValue(adf).
addOnSuccessListener(aVoid ->
isRef.child("Users").child("info").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot1) {
Users minfo = snapshot1.getValue(Users.class);
if (minfo!=null){
String mname = minfo.username;
String mid = minfo.uid;
addFriends madf = new addFriends(mid,mname);
isRef.child("Users").child(uid2).child("Friends").setValue(madf)
.addOnSuccessListener(aVoid1 -> {
isRef.child("Users").child(uid).child("Friends-request").child(uid2).removeValue();
isRef.child("Users").child(uid2).child("Friends-request").child(uid).removeValue();
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getContext(),Toast.LENGTH_SHORT).show();
}
}));
viewHolder.del.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isRef.child("Users").child(uid).child("Friends-request").child(uid2).removeValue();
isRef.child("Users").child(uid2).child("Friends-request").child(uid).removeValue();
}
});
// Return the completed view to render on screen
return convertView;
}
}
这里我在列表视图的活动中调用它:
private void getReqs() {
DatabaseReference isRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Friends-request");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (reqstemp.size() > 0){reqstemp.clear();}
for (DataSnapshot ds : snapshot.getChildren()){
addFriends reqs = ds.getValue(addFriends.class);
if (reqs!=null) {
reqstemp.add(reqs);
}
}
reqsadapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Friends.this,Toast.LENGTH_SHORT).show();
}
};
isRef.addValueEventListener(valueEventListener);
}
顺便说一下,这里是来自适配器的元素代码:
<?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:layout_height="96dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:id="@+id/cardViewsdd"
android:layout_width="86dp"
android:layout_height="86dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
app:cardBackgroundColor="@color/common_google_signin_btn_text_dark_disabled"
app:cardCornerRadius="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/uImgreqs"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:src="@mipmap/ic_launcher_round"
android:contentDescription="@string/todo" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tvDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text=""
android:textSize="20sp" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/ibAdd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@android:drawable/ic_input_add" />
<ImageView
android:id="@+id/ibDel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/ic_dialog_close_light" />
</LinearLayout>
</LinearLayout>
添加按钮应该互相记录两个用户的id,并相应地删除请求。 uImgreqs只是用户的一张照片,两个textviews都是用户名的用户数据等。如果有什么需要补充的,我很乐意补充,因为我很绝望,期限快到了
解决方法
上帝,呜呜呜呜呜呜。 Android Studio,我崇拜你。
我找到了一个解决方案,结果发现问题是由于 (v ->
,我或工作室将 OnClick 函数转换为 lambda,因此它返回了一个空对象...... 3 天半...... . 我花了三天多的时间,omg
viewHolder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {});
不,这不是赢
呃,不,它并没有完全解决问题,现在在每个 imageView .setOnClicklistener 上抛出一个错误......
我发现了问题,我写错了标签:
convertView = inflater.inflate(R.layout.friends_item,parent,false);
非常不起眼的线条,面掌
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。