Reputation: 580
fragment class
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;
}
}
when I run the app it stops
logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rashim12000.waytofreedom.swasthyatipot/com.rashim12000.waytofreedom.swasthyatipot.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.rashim12000.waytofreedom.swasthyatipot.helper.DatabaseHandler.getAllData()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.rashim12000.waytofreedom.swasthyatipot.helper.DatabaseHandler.getAllData()' on a null object reference at com.rashim12000.waytofreedom.swasthyatipot.fragment.My_Health.onCreateView(My_Health.java:55) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
Upvotes: 2
Views: 56
Reputation: 81
the recycler view
is not showing anything cause the items.size
in your recyclerview adapter
must be returning 0.
Upvotes: 1
Reputation: 35661
You are calling
db.getAllData()
in the line
mRecyclerView.setAdapter(new HomeCardAdapter(db.getAllData(),R.layout.cardview_home));
but have not initialised db
Upvotes: 2