0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Siva
  • 9,043
  • 12
  • 40
  • 63

4 Answers4

1

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.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Thanks gunaseelan... but what is the importance of context in android.. can you please as I am a newbie and try to learn on my own – Siva Apr 26 '13 at 05:56
  • I appreciate you for your interesting. But you try to find it by yourself. Why because always there is a question will occure while you trying to asking help to someone `What you have tried?`. So always you should have answer for this question. Anyway the answer for your question is [link1](http://stackoverflow.com/questions/3572463/what-is-context-in-android) and [link2](http://developer.android.com/reference/android/content/Context.html). – Gunaseelan Apr 26 '13 at 06:06
  • Thanks Gunaseelan... I am trying a lot for answers may be it may take some time for me to get grip on android anyway thanks for your suggestion – Siva Apr 26 '13 at 06:13
0

Use,

Toast.makeText(getApplicationContext(), getEID, Toast.Long_SHORT).show();
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
  • Just a heads-up: this answer was automatically flagged as low quality due to its length. Can you edit to add a little more? Explain what it does, or how it solves the problem? – luser droog Apr 26 '13 at 06:04
0
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;
}
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • Thanks Lazy Ninja.... but will this toast displayed on opening screen as I am not writing this class in main acitivty also if possivle can you please give me some idea on what is context and how it works. – Siva Apr 26 '13 at 05:53
0

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.

itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31