应用程序因尝试在空对象引用上调用虚拟方法“”而崩溃
App crashes because of attempt to invoke virtual method ' ' on a null object reference
当我在 Recyclerview 中单击图像时,我试图让自定义对话框出现。但是,尽管编译时代码没有明显错误,但当我单击菜单项进入页面时,我的应用程序一直崩溃。 logcat 中的错误表示:
java.lang.NullPointerException: Attempt to invoke virtual method 'void de.hdodenhof.circleimageview.CircleImageView.setImageResource(int)' on a null object reference
at com.example.android.myndapplication.adapter.UserAdapter.onBindViewHolder(UserAdapter.java:61)
at com.example.android.myndapplication.adapter.UserAdapter.onBindViewHolder(UserAdapter.java:25)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:572)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3686)
下面我插入了我目前关于此事的代码:
** UserAdapter.java**
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.myndapplication.R;
import com.example.android.myndapplication.model.User;
import org.w3c.dom.Text;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> userList;
Dialog myDialog;
public UserAdapter(Context mContext, List<User> userList) {
this.mContext = mContext;
this.userList = userList;
}
@NonNull
@Override
public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_list_item, parent, false);
final ViewHolder ViewHolder = new ViewHolder(view);
// Dialog ini
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_profile);
CircleImageView iv_profile = (CircleImageView) myDialog.findViewById(R.id.dialog_profile_image);
TextView tv_username = (TextView) myDialog.findViewById(R.id.dialog_profile_username);
TextView tv_date = (TextView) myDialog.findViewById(R.id.dialog_profile_date);
ImageView iv_content = (ImageView) myDialog.findViewById(R.id.dialog_content_image);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, final int position) {
final User user = userList.get(position);
holder.iv_profile.setImageResource(user.getProfileImage());
holder.userName.setText(user.getUserName());
holder.userDate.setText(user.getUserDate());
holder.image_content.setImageResource(user.getImage());
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "Test Click"+userList.get(position), Toast.LENGTH_SHORT).show();
myDialog.show();
}
});
}
@Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CardView item;
private CircleImageView iv_profile;
private TextView userName;
private TextView userDate;
private ImageView image_content;
private ImageView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.img_bookmarks);
textView = itemView.findViewById(R.id.title_bookmarks);
item = (CardView) itemView.findViewById(R.id.profile_item);
iv_profile = (CircleImageView) itemView.findViewById(R.id.dialog_profile_image);
userName = (TextView) itemView.findViewById(R.id.dialog_profile_username);
userDate = (TextView) itemView.findViewById(R.id.dialog_profile_date);
image_content = (ImageView) itemView.findViewById(R.id.dialog_content_image);
//test
//itemView.setOnClickListener((View.OnClickListener) this);
}
}
}
User.java
public class User {
private int image;
private String Title;
private String UserName;
private int profileImage;
private String userDate;
public String getUserDate() {
return userDate;
}
public void setUserDate(String userDate) {
this.userDate = userDate;
}
public User(int image, String Title, String userName, int profileImage, String userDate) {
this.image = image;
this.Title = Title;
this.UserName = userName;
this.profileImage = profileImage;
this.userDate = userDate;
}
public String getUserName() {
return UserName;
}
public int getProfileImage() {
return profileImage;
}
public void setProfileImage(int profileImage) {
this.profileImage = profileImage;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return Title;
}
public void setUserName(String Title) {
this.Title = Title;
}
}
dialog_profile.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"
android:layout_width="400dp"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/dialog_profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@mipmap/ic_launcher_round"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dialog_profile_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/username"
android:textSize="10sp"
app:layout_constraintStart_toEndOf="@+id/dialog_profile_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dialog_profile_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="date"
android:textSize="8sp"
app:layout_constraintStart_toEndOf="@+id/dialog_profile_image"
app:layout_constraintTop_toBottomOf="@+id/dialog_profile_username" />
<ImageView
android:id="@+id/dialog_content_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="70dp"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已经检查了我的 ID,并试图确保我有 setContentView,正如我所看到的与我的问题类似的问题。但是,似乎没有任何效果,我也无法真正发现错误。希望对您有所帮助!
我不确定,但是在“OnBindViewHolder”中,您可以尝试从视图持有者的 itemView 访问“iv_profile”吗?
我的意思是这样的:
holder.itemView.iv_profile.setImageResource(user.getProfileImage());
而不是这个:
holder.iv_profile.setImageResource(user.getProfileImage());
我的假设是您的引用在视图中,而不是绑定到持有者本身。
您没有收到编译时错误的原因是您的那些 ID 在 dialog_profile.xml
布局中有效,但回收站项目布局不同,recycler_list_item
请参阅您的 onCreateViewHolder
.
这就是 CircleImageView
上出现运行时 NullPointerException 的原因,因为那不是您的 recycler_list_item
的一部分,它当前在创建适配器时膨胀。
注意:始终确保您在 viewholder 中使用的视图在布局适配器中正在膨胀,因为如果您正在这样做,您将不会出现编译时错误错误,但在运行时,它会崩溃。
当我在 Recyclerview 中单击图像时,我试图让自定义对话框出现。但是,尽管编译时代码没有明显错误,但当我单击菜单项进入页面时,我的应用程序一直崩溃。 logcat 中的错误表示:
java.lang.NullPointerException: Attempt to invoke virtual method 'void de.hdodenhof.circleimageview.CircleImageView.setImageResource(int)' on a null object reference
at com.example.android.myndapplication.adapter.UserAdapter.onBindViewHolder(UserAdapter.java:61)
at com.example.android.myndapplication.adapter.UserAdapter.onBindViewHolder(UserAdapter.java:25)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:572)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3686)
下面我插入了我目前关于此事的代码:
** UserAdapter.java**
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.myndapplication.R;
import com.example.android.myndapplication.model.User;
import org.w3c.dom.Text;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> userList;
Dialog myDialog;
public UserAdapter(Context mContext, List<User> userList) {
this.mContext = mContext;
this.userList = userList;
}
@NonNull
@Override
public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_list_item, parent, false);
final ViewHolder ViewHolder = new ViewHolder(view);
// Dialog ini
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_profile);
CircleImageView iv_profile = (CircleImageView) myDialog.findViewById(R.id.dialog_profile_image);
TextView tv_username = (TextView) myDialog.findViewById(R.id.dialog_profile_username);
TextView tv_date = (TextView) myDialog.findViewById(R.id.dialog_profile_date);
ImageView iv_content = (ImageView) myDialog.findViewById(R.id.dialog_content_image);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, final int position) {
final User user = userList.get(position);
holder.iv_profile.setImageResource(user.getProfileImage());
holder.userName.setText(user.getUserName());
holder.userDate.setText(user.getUserDate());
holder.image_content.setImageResource(user.getImage());
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "Test Click"+userList.get(position), Toast.LENGTH_SHORT).show();
myDialog.show();
}
});
}
@Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CardView item;
private CircleImageView iv_profile;
private TextView userName;
private TextView userDate;
private ImageView image_content;
private ImageView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.img_bookmarks);
textView = itemView.findViewById(R.id.title_bookmarks);
item = (CardView) itemView.findViewById(R.id.profile_item);
iv_profile = (CircleImageView) itemView.findViewById(R.id.dialog_profile_image);
userName = (TextView) itemView.findViewById(R.id.dialog_profile_username);
userDate = (TextView) itemView.findViewById(R.id.dialog_profile_date);
image_content = (ImageView) itemView.findViewById(R.id.dialog_content_image);
//test
//itemView.setOnClickListener((View.OnClickListener) this);
}
}
}
User.java
public class User {
private int image;
private String Title;
private String UserName;
private int profileImage;
private String userDate;
public String getUserDate() {
return userDate;
}
public void setUserDate(String userDate) {
this.userDate = userDate;
}
public User(int image, String Title, String userName, int profileImage, String userDate) {
this.image = image;
this.Title = Title;
this.UserName = userName;
this.profileImage = profileImage;
this.userDate = userDate;
}
public String getUserName() {
return UserName;
}
public int getProfileImage() {
return profileImage;
}
public void setProfileImage(int profileImage) {
this.profileImage = profileImage;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return Title;
}
public void setUserName(String Title) {
this.Title = Title;
}
}
dialog_profile.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"
android:layout_width="400dp"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/dialog_profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@mipmap/ic_launcher_round"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dialog_profile_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/username"
android:textSize="10sp"
app:layout_constraintStart_toEndOf="@+id/dialog_profile_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dialog_profile_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="date"
android:textSize="8sp"
app:layout_constraintStart_toEndOf="@+id/dialog_profile_image"
app:layout_constraintTop_toBottomOf="@+id/dialog_profile_username" />
<ImageView
android:id="@+id/dialog_content_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="70dp"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已经检查了我的 ID,并试图确保我有 setContentView,正如我所看到的与我的问题类似的问题。但是,似乎没有任何效果,我也无法真正发现错误。希望对您有所帮助!
我不确定,但是在“OnBindViewHolder”中,您可以尝试从视图持有者的 itemView 访问“iv_profile”吗?
我的意思是这样的:
holder.itemView.iv_profile.setImageResource(user.getProfileImage());
而不是这个:
holder.iv_profile.setImageResource(user.getProfileImage());
我的假设是您的引用在视图中,而不是绑定到持有者本身。
您没有收到编译时错误的原因是您的那些 ID 在 dialog_profile.xml
布局中有效,但回收站项目布局不同,recycler_list_item
请参阅您的 onCreateViewHolder
.
这就是 CircleImageView
上出现运行时 NullPointerException 的原因,因为那不是您的 recycler_list_item
的一部分,它当前在创建适配器时膨胀。
注意:始终确保您在 viewholder 中使用的视图在布局适配器中正在膨胀,因为如果您正在这样做,您将不会出现编译时错误错误,但在运行时,它会崩溃。