java - activity crashes when going back home -
i have edit task activity single edittext view. when click button on action bar, database updated , log registers it, activity crashes while going back. main activity display text added in task activity. here code home button:
public class newtask extends activity { protected taskerdbhelper db; myadapter adapt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_new_task); db = new taskerdbhelper(this); setupactionbar(); } private void setupactionbar() { getactionbar().setdisplayhomeasupenabled(true); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.new_task, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // id represents home or button. in case of // activity, button shown. use navutils allow users // navigate 1 level in application structure. // more details, see navigation pattern on android design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // edittext t = (edittext) findviewbyid(r.id.edittext1); string s = t.gettext().tostring(); if (s.equalsignorecase("")) { toast.maketext(this, "enter task description first!!", toast.length_long); } else { task task = new task(s, 0); db.addtask(task); log.d("tasker", "data added"); //t.settext(""); //adapt.add(task); adapt.notifydatasetchanged(); } setresult(result_ok); finish(); return true; } return super.onoptionsitemselected(item); } }
whats wrong?
here log:
08-16 17:56:05.117: e/androidruntime(6162): fatal exception: main 08-16 17:56:05.117: e/androidruntime(6162): java.lang.nullpointerexception 08-16 17:56:05.117: e/androidruntime(6162): @ com.example.tasker.newtask.onoptionsitemselected(newtask.java:62) 08-16 17:56:05.117: e/androidruntime(6162): @ android.app.activity.onmenuitemselected(activity.java:2552) 08-16 17:56:05.117: e/androidruntime(6162): @ com.android.internal.widget.actionbarview$3.onclick(actionbarview.java:167) 08-16 17:56:05.117: e/androidruntime(6162): @ android.view.view.performclick(view.java:4204) 08-16 17:56:05.117: e/androidruntime(6162): @ android.view.view$performclick.run(view.java:17355) 08-16 17:56:05.117: e/androidruntime(6162): @ android.os.handler.handlecallback(handler.java:725) 08-16 17:56:05.117: e/androidruntime(6162): @ android.os.handler.dispatchmessage(handler.java:92) 08-16 17:56:05.117: e/androidruntime(6162): @ android.os.looper.loop(looper.java:137) 08-16 17:56:05.117: e/androidruntime(6162): @ android.app.activitythread.main(activitythread.java:5234) 08-16 17:56:05.117: e/androidruntime(6162): @ java.lang.reflect.method.invokenative(native method) 08-16 17:56:05.117: e/androidruntime(6162): @ java.lang.reflect.method.invoke(method.java:525) 08-16 17:56:05.117: e/androidruntime(6162): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:799) 08-16 17:56:05.117: e/androidruntime(6162): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:566) 08-16 17:56:05.117: e/androidruntime(6162): @ dalvik.system.nativestart.main(native method)
try checking if adapter null first
if(adapt != null) { adapt.notifydatasetchanged(); } else { // initialize , other stuff } and in declaration, declare adapt this
public myadapter adapt; i can see using custom adapter class. think problem may there in notifydatasetchanged() method. can post it?
Comments
Post a Comment