android - How to show toast in second java file? -
i have been reading forum days , have found no answer problem.
i'm having problem getting java.lang.nullpointerexception when try show toast.
in android application have myworkactivity.java main activity , statistika.java intended calculate statistics based on data database.
inside class myworkactivity (public class myworkactivity extends activity implements onclicklistener) have menu item statistika.
when press on item want show layout statistika.xml data statistika.java this:
public class myworkactivity extends activity implements onclicklistener { //menu item statistika else if(item.getitemid()==r.id.statistika) { //layout statistika.xml setcontentview(r.layout.statistika); statistika nova_statistika = new statistika(); nova_statistika.rsatidanasjucemjesec(sdatumzaprikaz); } }
but have been receiving java.lang.nullpointerexception on dbjucedanas = this.openorcreatedatabase(databaseinfo.ime_baze, mode_private,null);
have commented database part , tried toast still java.lang.nullpointerexception.
this statistika.java:
public class statistika extends activity { public void rsatidanasjucemjesec (string sdatumzaracunanje) { toast.maketext(getbasecontext(), "toast working! ", toast.length_long).show(); //računanje radnih sati za prikazani dan // sqlitedatabase dbjucedanas = null; // dbjucedanas = this.openorcreatedatabase(databaseinfo.ime_baze, mode_private,null); // // //filtriranje rekordseta po datumu // cursor cjucedanas = dbjucedanas.rawquery("select * " + databaseinfo.ime_tabele + " " + databaseinfo.datum + " '%" + myworkactivity.sdatumzaprikaz + "'", null); // // if (cjucedanas !=null && cjucedanas.getcount() > 0) // { // //ako postoje upisi za datum trenutnog upisa // int column4 = cjucedanas.getcolumnindex("r_vrijeme"); // // //sumiranje svih radnih vremena od danas // frvdanas=float.valueof(0); // // { // cjucedanas.movetofirst(); // frvdanas=frvdanas+cjucedanas.getfloat(column4); // // }while (cjucedanas.movetonext()); // } } }
this logcat:
04-25 12:33:15.020: e/androidruntime(927): fatal exception: main 04-25 12:33:15.020: e/androidruntime(927): java.lang.nullpointerexception 04-25 12:33:15.020: e/androidruntime(927): @ android.content.contextwrapper.getresources(contextwrapper.java:80) 04-25 12:33:15.020: e/androidruntime(927): @ android.widget.toast.<init>(toast.java:89) 04-25 12:33:15.020: e/androidruntime(927): @ android.widget.toast.maketext(toast.java:231) 04-25 12:33:15.020: e/androidruntime(927): @ mywork.amicus.ba.statistika.rsatidanasjucemjesec(statistika.java:69) 04-25 12:33:15.020: e/androidruntime(927): @ mywork.amicus.ba.myworkactivity.onoptionsitemselected(myworkactivity.java:3028) 04-25 12:33:15.020: e/androidruntime(927): @ android.app.activity.onmenuitemselected(activity.java:2205) 04-25 12:33:15.020: e/androidruntime(927): @ com.android.internal.policy.impl.phonewindow.onmenuitemselected(phonewindow.java:748)
i have tried following:
toast.maketext(this, "toast working! ", toast.length_long).show(); toast.maketext(statistika.this, "toast working! ", toast.length_long).show();
and many other combinations doesn't work.
as can understand, problem context.
what need able show toast message , access database statistika.java?
try code public class myworkactivity extends activity implements onclicklistener { //menu item statistika else if(item.getitemid()==r.id.statistika) { //layout statistika.xml setcontentview(r.layout.statistika); statistika nova_statistika = new statistika(myworkactivity.this); nova_statistika.rsatidanasjucemjesec(sdatumzaprikaz); } } public class statistika extends activity { context context; public statistika(myworkactivity mainactivity) { // todo auto-generated constructor stub context = mainactivity; } public void rsatidanasjucemjesec (string sdatumzaracunanje) { toast.maketext(context, "toast working! ", toast.length_long).show(); } }