我有一個RecyclerView從Firebase數據庫獲取數據。當RecyclerView到達數據結束喂應用程序崩潰與以下:RecyclerView在數據結束時崩潰
logcat的文件:
09-27 15:30:12.826 30184-30184/com.pctoolmanconsulting.trackmyweight E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pctoolmanconsulting.trackmyweight, PID: 30184
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference
at com.pctoolmanconsulting.trackmyweight.ProgressViewHolder.bind(ProgressViewHolder.java:40)
at com.pctoolmanconsulting.trackmyweight.ProgressAdapter.onBindViewHolder(ProgressAdapter.java:41)
at com.pctoolmanconsulting.trackmyweight.ProgressAdapter.onBindViewHolder(ProgressAdapter.java:15)
我想不通的地方我應該檢查空值。在我的ViewHolder類的綁定方法中或在適配器類中。
有人可以幫助指向正確的方向嗎?
謝謝。
我ViewHolder綁定方法:
public void bind (ImageProgress progress){
name.setText(progress.name);
if(progress.progressDetail.startsWith("https://firebasestorage.googleapis.com/") || progress.progressDetail.startsWith("content://")){
progressDetailMessage.setVisibility(View.INVISIBLE);
image.setVisibility(View.VISIBLE);
Glide.with(activity).load(progress.progressDetail).fitCenter().into(image);
} else {
progressDetailMessage.setVisibility(View.VISIBLE);
image.setVisibility(View.GONE);
progressDetailMessage.setText(progress.progressDetail);
}
}
適配器類:
public class ProgressAdapter extends RecyclerView.Adapter<ProgressViewHolder> {
private static final String TAG ="ProgressAdapter";
private final Activity activity;
List<ImageProgress> progressDetails = new ArrayList<>();
public ProgressAdapter(Activity activity){
this.activity = activity;
}
public void addProgress(ImageProgress progress){
progressDetails.add(progress);
notifyItemInserted(progressDetails.size());
}
@Override
public ProgressViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
return new ProgressViewHolder(activity, activity.getLayoutInflater().inflate(android.R.layout.two_line_list_item,
parent, false));
}
@Override
public void onBindViewHolder(ProgressViewHolder holder, int position){
holder.bind(progressDetails.get(position));
}
@Override
public int getItemCount() {
return progressDetails.size();
}
}
您需要檢查爲空,因爲它似乎 –
我在哪裏檢查null是我需要幫助 – hozdaman
似乎progressdetail爲null。在調用startsWith方法之前檢查它是否爲空 – Pete