在 RecyclerView 中单击删除按钮时出现空点异常
Getting Null point Exception when Clicking Delete Button in RecyclerView
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
myViewHolder.mName.setText(downModels.get(i).getName());
myViewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadTask();
}
});
myViewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pdfName = downModels.get(i).getName();
if(pdfName != null) {
pdfName = pdfName.replace("", "")+".pdf";
File file = new File(context.getFilesDir(), pdfName);
try {
if (file.exists())
file.delete();
Log.e("file","file"+file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
单击“删除”按钮时,应用程序崩溃。
2020-03-01 22:50:55.461 16800-16800/com.solutions.rsaggarwal E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.solutions.rsaggarwal, PID: 16800
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File
android.content.Context.getFilesDir()' on a null object reference
at com.solutions.rsaggarwal.MyAdapter.onClick(MyAdapter.java:70)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access00(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
您在适配器 class 中作为参数传递的 context
似乎为空。作为替代方案,您可以使用附加到 itemView
的上下文,如下所示。
Context context = myViewHolder.itemView.getContext();
File file = new File(context.getFilesDir(), pdfName);
try {
if (file.exists())
file.delete();
Log.e("file","file"+file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
myViewHolder.mName.setText(downModels.get(i).getName());
myViewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadTask();
}
});
myViewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pdfName = downModels.get(i).getName();
if(pdfName != null) {
pdfName = pdfName.replace("", "")+".pdf";
File file = new File(context.getFilesDir(), pdfName);
try {
if (file.exists())
file.delete();
Log.e("file","file"+file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
单击“删除”按钮时,应用程序崩溃。
2020-03-01 22:50:55.461 16800-16800/com.solutions.rsaggarwal E/AndroidRuntime: FATAL EXCEPTION: main Process: com.solutions.rsaggarwal, PID: 16800 java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference at com.solutions.rsaggarwal.MyAdapter.onClick(MyAdapter.java:70) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access00(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
您在适配器 class 中作为参数传递的 context
似乎为空。作为替代方案,您可以使用附加到 itemView
的上下文,如下所示。
Context context = myViewHolder.itemView.getContext();
File file = new File(context.getFilesDir(), pdfName);
try {
if (file.exists())
file.delete();
Log.e("file","file"+file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}