gpt4 book ai didi

java - RecyclerView onClickListener 显示错误

转载 作者:行者123 更新时间:2023-12-01 17:28:43 25 4
gpt4 key购买 nike

我正在使用 Java 和 Firebase 开发一个 Android 应用。我想将一个 onClickListener 添加到我的 recyclerView 中,将其定向到显示内容的其他 Activity 。我的问题是,当我向适配器添加 onClickListener 时,应用程序将崩溃。 Logcat 显示了这些错误。

2020-04-12 12:46:06.382 14440-14440/com.eNotification.getnotify E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eNotification.getnotify, PID: 14440
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.eNotification.getnotify.ImageAdapter.onBindViewHolder(ImageAdapter.java:50)
at com.eNotification.getnotify.ImageAdapter.onBindViewHolder(ImageAdapter.java:20)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
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:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
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:764)
at android.view.View.layout(View.java:19693)
at android.view.ViewGroup.layout(ViewGroup.java:6079)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2541)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2257)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6886)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
at android.view.Choreographer.doCallbacks(Choreographer.java:841)
at android.view.Choreographer.doFrame(Choreographer.java:772)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
2020-04-12 12:46:06.383 14440-14440/com.eNotification.getnotify E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

这是我的代码。

主要 Activity

package com.eNotification.getnotify;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

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

import com.google.firebase.auth.FirebaseAuth;
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.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;


public class MainActivityHomePage extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;

private ProgressBar mProgressCircle;

private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;

private SwipeRefreshLayout swipeRefreshLayout;

private FirebaseAuth mAuth;


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

mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mProgressCircle = findViewById(R.id.progressCircle);
mUploads = new ArrayList<>();

mAuth = FirebaseAuth.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("Messages");
read();

}

private void read(){
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivityHomePage.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(linearLayoutManager);
mAdapter = new ImageAdapter(MainActivityHomePage.this,mUploads);
mRecyclerView.setAdapter(mAdapter);
mProgressCircle.setVisibility(View.INVISIBLE);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivityHomePage.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
mProgressCircle.setVisibility(View.INVISIBLE);
}
});

swipeRefreshLayout = findViewById(R.id.swipeRefreshLoyout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(false);
}
});
}

/*@Override
protected void onStart() {
super.onStart();

FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser == null){
backToLogin();
}
}*/

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.slide_button,menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.other:
case R.id.logout:
logout();

}
return super.onOptionsItemSelected(item);
}

private void logout(){
mAuth.signOut();
backToLogin();
Toast.makeText(this, "Logout successful", Toast.LENGTH_SHORT).show();
}

private void backToLogin() {
Intent intent = new Intent(MainActivityHomePage.this,MainActivityLogIn.class);
startActivity(intent);
finish();
}
}

适配器

package com.eNotification.getnotify;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

private Context mContext;
private List<Upload> mUploads;

public ImageAdapter(Context context,List<Upload> uploads){
mContext = context;
mUploads = uploads;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.activity_card_view,parent,false);
return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
final Upload uploadCurrent = mUploads.get(position);
holder.hTitle.setText(uploadCurrent.getTitle());
holder.hDesc.setText(uploadCurrent.getDescription());
holder.hDate.setText(uploadCurrent.getDate());
holder.hTime.setText(uploadCurrent.getTime());
Picasso.get()
.load(uploadCurrent.getImage())
.fit()
.centerInside()
.into(holder.hImage);

holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,MainActivityEventView.class);
intent.putExtra("mTitle",uploadCurrent.getTitle());
intent.putExtra("mDescription",uploadCurrent.getDescription());
intent.putExtra("mDate",uploadCurrent.getDate());
intent.putExtra("mTime",uploadCurrent.getTime());
intent.putExtra("imageUrl",uploadCurrent.getImage());
intent.putExtra("sentDate",uploadCurrent.getSentDate());

mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mUploads.size();
}

public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView hTitle,hDesc,hDate,hTime;
public ImageView hImage;
public RelativeLayout parentLayout;

public ImageViewHolder(View itemView){
super(itemView);

hTitle = itemView.findViewById(R.id.hTitle);
hDesc = itemView.findViewById(R.id.hDescription);
hDate = itemView.findViewById(R.id.hDate);
hTime = itemView.findViewById(R.id.hTime);
hImage = itemView.findViewById(R.id.hImage);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}

模型

package com.eNotification.getnotify;

public class Upload {
private String mTitle;
private String mDescription;
private String mDate;
private String mTime;
private String imageUrl;
private String sentDate;
private String msgKey;

public Upload() {

}


public Upload(String title,String desc,String date,String time,String image, String sDate,String mKey){
/*if (title.trim().equals("") && desc.trim().equals("") && date.trim().equals("") && time.trim().equals("") && image.trim().equals("")){
title = "No Value";
desc = "No Value";
date = "No Value";
time = "No Value";
}*/
mTitle = title;
mDescription = desc;
mDate = date;
mTime = time;
imageUrl = image;
sentDate = sDate;
msgKey = mKey;
}

public String getTitle(){
return mTitle;
}

public void setTitle(String title){
mTitle = title;
}

public String getDescription(){
return mDescription;
}

public void setDescription(String desc){
mDescription = desc;
}

public String getDate(){
return mDate;
}

public void setDate(String date){
mDate = date;
}

public String getTime(){
return mTime;
}

public void setTime(String time){
mTime = time;
}

public String getImage(){
return imageUrl;
}

public void setImage(String image){
imageUrl = image;
}

public String getSentDate() {
return sentDate;
}

public void setSentDate(String sDate) {
this.sentDate = sDate;
}

public String getMsgKey() {
return msgKey;
}

public void setMsgKey(String msgKey) {
this.msgKey = msgKey;
}
}

此类在点击时显示recyclerview的内容。

package com.eNotification.getnotify;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.squareup.picasso.Picasso;

public class MainActivityEventView extends AppCompatActivity {
private static final String TAG = "GalleryActivity";

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


private void getIncomingIntent(){
if (getIntent().hasExtra("mTitle") && getIntent().hasExtra("mDescription")
&& getIntent().hasExtra("mDate") && getIntent().hasExtra("mTime")
&& getIntent().hasExtra("imageUrl") && getIntent().hasExtra("sentDate")){

String mTitle = getIntent().getStringExtra("mTitle");
String mDescription = getIntent().getStringExtra("mDescription");
String mDate = getIntent().getStringExtra("mDate");
String mTime = getIntent().getStringExtra("mTime");
String imageUrl = getIntent().getStringExtra("imageUrl");
String sentDate = getIntent().getStringExtra("sentDate");
setImage(mTitle,mDescription,mDate,mTime,imageUrl,sentDate);
}
}

private void setImage(String mTitle, String mDescription, String mDate,String mTime, String imageUrl, String sentDate){

TextView title = findViewById(R.id.eventTitle);
title.setText(mTitle);
TextView description = findViewById(R.id.eventDescription);
description.setText(mDescription);
TextView date = findViewById(R.id.eventDate);
date.setText(mDate);
TextView time = findViewById(R.id.eventTime);
time.setText(mTime);
TextView sentDay = findViewById(R.id.eventSentDate);
sentDay.setText(sentDate);

ImageView image = findViewById(R.id.eventImage);
Picasso.get().load(imageUrl).into(image);

}

}

单击时用于显示 RecyclerView 内容的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:padding="10dp"
tools:context=".MainActivityEventView">

<ImageView
android:id="@+id/eventImage"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_image_black_24dp" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="370dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/eventDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/eventTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="15sp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/eventDate"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/eventTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:lines="2"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/eventDate" />

<TextView
android:id="@+id/eventDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:text="TextView"
android:lines="40"
android:minLines="10"
android:gravity="top|start"
android:scrollbars="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/eventTitle" />

<TextView
android:id="@+id/eventSentDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/eventDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

activity_card_view

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardBackgroundColor="@android:color/white"
android:layout_marginTop="10dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/silver"
android:padding="5dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ImageView
android:id="@+id/hImage"
android:layout_width="250dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<TextView
android:id="@+id/hTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:maxLines="1"
android:paddingStart="10dp"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hTime" />

<TextView
android:id="@+id/hDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:maxLines="2"
android:paddingStart="10dp"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hTitle" />

<TextView
android:id="@+id/hDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Date"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/hTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@+id/hDate"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

activity_home_page XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ProgressBar
android:id="@+id/progressCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLoyout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:reverseLayout="true"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

我是初学者,不知道如何解决这个问题。任何帮助都会很棒。

最佳答案

试试这个,也许这会有所帮助不要在 ImageViewHolder 中声明变量 RelativeLayout ParentLayout,而是在 ImageViewHolder 构造函数中存储 mView = itemView 后声明 View mView然后在onBindViewHolder方法中holder.mView.setOnClickListener

关于java - RecyclerView onClickListener 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61168164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com