Reputation: 9101
I have code like this
package collfw;
public class A {
int Eid;
Context c1;
public void setEid(int id) {
if (id < 0) {
Eid = 0;
} else {
Eid = id;
}
}
public int getEid() {
return Eid;
}
public contentvalues adddata()
{
contentvalues cv=new contentvalues()
cv.put(ID,getEid());
return cv;
}
public void retrivedata() {
cursor c = db.rawquery("select * from employee");
**Toast.maketext(c1, getEID, toast.Long_SHORT).show();**
}
}
Here toast is giving me the error and logcat shows println can't be null and if in place of c1 if I use "context" then it is not accepting, Can anyone please explain me what is context and how can I use it here.
Upvotes: 0
Views: 96
Reputation: 4638
Use like below if you will call retrievedata method directly from other activity or classed
public void retrivedata(Context c1) {
cursor c = db.rawquery("select * from employee");
**Toast.maketext(c1, getEID, toast.Long_SHORT).show();**
}
Else Create the constructor for class A with the Context as the argument.
Context c1;
Public A(Context ccc)
{
c1=ccc;
}
then use the c1 wherever in your Class A
Hope this will help you.
Upvotes: 0
Reputation: 15525
You have to initialize your c1. Then only it will work.
public A(Context context) {
c1 = context;
}
Because toast is like a message it will dispaly on Activity. So you have to initialize your context
with your activity
's context
I hope this will help you.
Upvotes: 1
Reputation: 22527
Toast.maketext(c1, getEID, toast.Long_SHORT).show();
c1 is probably not set. At least I dont see it set on your class. Add something like this:
public A(Context ctx){
c1 = ctx;
}
Upvotes: 0
Reputation: 2719
Use,
Toast.makeText(getApplicationContext(), getEID, Toast.Long_SHORT).show();
Upvotes: 0