来自SQLIte的数据未在回收站视图

时间:2015-11-19 14:13:27

标签: android sqlite

片段类

public class My_Health extends Fragment {

DatabaseHandler db;

protected RecyclerView mRecyclerView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.home, container, false);

    RecyclerView recList = (RecyclerView) v.findViewById(R.id.cardlist_home);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    mRecyclerView = (RecyclerView) v.findViewById(R.id.cardlist_home);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setAdapter(new HomeCardAdapter(db.getAllData(), R.layout.cardview_home));


    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.addrecordfab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), AddRecord.class);
            startActivity(intent);
        }
    });
    return v;
}
}

DatabaseHandler.java

public List<UserInfo> getAllData() {
    List<UserInfo> list = new ArrayList<>();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    UserInfo bean = null;

    while (cursor.moveToNext()) {
        int index0 = cursor.getColumnIndex(KEY_DATE);
        int index1 = cursor.getColumnIndex(KEY_MOBILE_NO);
        int index2 = cursor.getColumnIndex(KEY_DIAGNOSIS);

        String date = cursor.getString(index0);
        String mobile_no = cursor.getString(index1);
        String heading = cursor.getString(index2);
        bean = new UserInfo(date,mobile_no,heading);
        list.add(bean);
    }
    return list;
}

}

当我运行应用程序时,它会停止

logcat的

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.rashim12000.waytofreedom.swasthyatipot / com.rashim12000.waytofreedom.swasthyatipot.MainActivity}:java.lang.NullPointerException:尝试调用虚方法&#39; java .util.List com.rashim12000.waytofreedom.swasthyatipot.helper.DatabaseHandler.getAllData()&#39;在null对象引用上                                                                                                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)                                                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)                                                                                                   在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                                   在android.os.Looper.loop(Looper.java:135)                                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5257)                                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)                                                                                                引起:java.lang.NullPointerException:尝试调用虚方法&#39; java.util.List com.rashim12000.waytofreedom.swasthyatipot.helper.DatabaseHandler.getAllData()&#39;在null对象引用上                                                                                                   在com.rashim12000.waytofreedom.swasthyatipot.fragment.My_Health.onCreateView(My_Health.java:55)                                                                                                   在android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)

2 个答案:

答案 0 :(得分:2)

您正在致电

db.getAllData()

mRecyclerView.setAdapter(new HomeCardAdapter(db.getAllData(),R.layout.cardview_home));

但尚未初始化db

答案 1 :(得分:1)

recycler view未显示任何内容,导致items.size中的recyclerview adapter必须返回0。