通过SQlite向recyclerview添加数据

Adding data to recyclerview through SQlite

我正尝试在 recyclerview 中从数据库中一一显示数据,但应用程序停止工作并出现空点异常。 这是详细的代码,因为我找不到错误。 我试过使用 stringBuffer 但它获取所有数据并在单个 cardview 中显示所有数据。

GetterSetterClass.java

public class GetterAndSetterClass  {
private String name;
private String details;

public GetterAndSetterClass( String name, String details) {
    this.name = name;
    this.details = details;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}
}

cartAdapter.java(正在使用 AdapterClass)

public class cartAdapter extends RecyclerView.Adapter<cartAdapter.cartVewHolder> 
 {

 private ArrayList<GetterAndSetterClass> namesLists;

public cartAdapter(ArrayList<GetterAndSetterClass> namesLists) {
    this.namesLists = namesLists;
}

@Override
public cartVewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.singletext,parent,false);
    cartVewHolder cartVewHolder=new cartVewHolder(view);
    return cartVewHolder;
}
@Override
public void onBindViewHolder(cartVewHolder holder, int position) {

   final GetterAndSetterClass getterAndSetterClass=namesLists.get(position);
   holder.textView.setText(getterAndSetterClass.getName());
    holder.textView2.setText(getterAndSetterClass.getDetails());
}
@Override
public int getItemCount() {
    return namesLists.size();
}

public class cartVewHolder extends RecyclerView.ViewHolder {
    TextView textView,textView2;
        public cartVewHolder(View itemView) {
        super(itemView);
        textView=itemView.findViewById(R.id.textviewCard);
        textView2=itemView.findViewById(R.id.textview2Card);

    }
}
}

cart.java(正在使用 MainActivity)

DatabaseHelperClass databaseHelper;
RecyclerView recyclerView;
GetterAndSetterClass getterAndSetterClass;
cartAdapter cartAdapter;
ArrayList<GetterAndSetterClass> arrayList= new ArrayList<>();

onCreateMethod

recyclerView=findViewById(R.id.cart_recyclerview);
    databaseHelper=new DatabaseHelperClass(this);
    Cursor cursor=databaseHelper.getAllData();
    if (cursor!=null && cursor.getCount()>0){
        while (cursor.moveToNext()){
            getterAndSetterClass.setName(cursor.getString(1)+"\n");
            getterAndSetterClass.setDetails(cursor.getString(2)+"\n\n");
        }
         arrayList.add(getterAndSetterClass);
    }
    else {
        Toast.makeText(getApplicationContext(),"No Data to retrived",Toast.LENGTH_SHORT).show();
    }
    recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
    cartAdapter=new cartAdapter(arrayList);
    recyclerView.setAdapter(cartAdapter);

错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.uzairmughal.parcelable/com.example.uzairmughal.parcelable.Cart}: java.lang.NullPointerException
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
                  at android.app.ActivityThread.access0(ActivityThread.java:148)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:5494)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:525)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.NullPointerException
                  at com.example.uzairmughal.parcelable.Cart.onCreate(Cart.java:42)
                  at android.app.Activity.performCreate(Activity.java:5234)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 
                  at android.app.ActivityThread.access0(ActivityThread.java:148) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319) 
                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:5494) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:525) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691) 
                  at dalvik.system.NativeStart.main(Native Method) 

应用程序终止。

在调用列表中的 size() 方法之前,您需要处理 null 情况 namesLists.size()

使用以下代码

更改您的适配器getItemCount()
@Override
public int getItemCount() {
  if(namesLists!=null)    
     return namesLists.size();
  else
    return 0;
}

您没有创建 GetterAndSetterClass 的对象并将值设置为 getterAndSetterClass

使用此代码

while (cursor.moveToNext()){
   GetterAndSetterClass getterAndSetterClass = new GetterAndSetterClass(cursor.getString(1),cursor.getString(2));
   arrayList.add(getterAndSetterClass);
}

适配器初始化时传递空列表,不需要检查是否为空