I am trying to open the database and retrieve the data and display the data as cardview in RecyclerView. I dont know much about context. I am getting the error in context. also hoe to use getActivity() most answers talked about it.
public class getList extends Activity{
public SQLiteDatabase db;
public List<String> array = new ArrayList<String>();
public List<String> array1 = new ArrayList<String>();
public getList(){
//REQUIRED PUBLIC CONST
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public List<String> getListofevents() {
this.db = this.openOrCreateDatabase("events", Context.MODE_PRIVATE, null);
Cursor crs = db.rawQuery("SELECT * FROM event", null);
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("name"));
Log.e("The string is : ",uname);
array.add(uname);
}
db.close();
return array;
}
public List<String> getListofstatus() {
this.db = this.openOrCreateDatabase("events", Context.MODE_PRIVATE, null);
Cursor crs = db.rawQuery("SELECT * FROM event", null);
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("status"));
Log.e("The string is : ",uname);
array1.add(uname);
}
db.close();
return array1;
}
}
"events" is the database and "event" is a table. This is used to create adapter ContactAdapter
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
public List<String> arrayevent= new ArrayList<String>();
public List<String> arraystatus= new ArrayList<String>();
public getList tmp;
public ContactAdapter() {
tmp = new getList();
arrayevent = tmp.getListofevents();
arraystatus = tmp.getListofstatus();
}
@Override
public int getItemCount() {
return arrayevent.size();
}
@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
String ce = arrayevent.get(i);
contactViewHolder.eventName.setText(ce);
String cs = arraystatus.get(i);
contactViewHolder.eventName.setText(cs);
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_layout, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView eventName;
protected TextView estatus;
public ContactViewHolder(View v) {
super(v);
eventName = (TextView) v.findViewById(R.id.eventName);
estatus = (TextView) v.findViewById(R.id.eventStatus);
}
}
}
When i am running for first time the app crashes and gives following null exception error.
04-02 03:10:59.308 32071-32071/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mukul.workingtabs, PID: 32071
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory)' on a null object reference
at com.example.mukul.workingtabs.getList.getListofevents(getList.java:38)
at com.example.mukul.workingtabs.ContactAdapter.<init>(ContactAdapter.java:22)
at com.example.mukul.workingtabs.OneFragment.onCreateView(OneFragment.java:38)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:731)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Meth
Thanks for help in advance :)
Aucun commentaire:
Enregistrer un commentaire