java - Android app crash when try to overwrite text in a TextView from other class -
i try change 3 textviews in class called userprofile() calling method update() class updateprofile(), class userprofile this:
package com.safm; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.textview; public class userprofile extends activity { textview username, usersurname, useremail; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_profile); username = (textview) findviewbyid(r.id.username); usersurname = (textview) findviewbyid(r.id.usersurname); useremail = (textview) findviewbyid(r.id.useremail); } public void updatebutton(view view){ intent = new intent(this, updateprofile.class); startactivity(i); } public void update(string nname, string nusername, string nemail){ system.out.println("2"); system.out.println(nname); username.settext(nname); usersurname.settext(nusername); useremail.settext(nemail); system.out.println("3"); } }
the updatebutton method invoke updateprofile class:
package com.safm; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.edittext; import android.widget.toast; public class updateprofile extends activity { edittext newusernametxt, newsurnametxt, newemailtxt; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_actualizarperfil); newusernametxt = (edittext) findviewbyid(r.id.newusernametxt); newsurnametxt = (edittext) findviewbyid(r.id.newsurnametxt ); newemailtxt = (edittext) findviewbyid(r.id.newemailtxt ); } public void updateinfo(view view){ string nname = newusernametxt .gettext().tostring(); string nsurname = newsurnametxt .gettext().tostring(); string nemail = newemailtxt .gettext().tostring(); if(nname.compareto("") != 0 && nsurname.compareto("") != 0 && nemail.compareto("") != 0){ userprofile profile = new userprofile(); system.out.println("1"); profile.update(nname, nsurname, nemail); system.out.println("4"); toast.maketext(this, "success, profile has been updated", toast.length_short).show(); }else{ toast.maketext(this, "you can't empty fields", toast.length_short).show(); } }
this logcat file, can see, first 2 flags printed correctly, error when try set text on textview, value of strings (nname, nsurname , nemail) not empty cause printed in console help!
05-14 21:43:31.140: i/system.out(878): 1 05-14 21:43:31.140: i/system.out(878): 2 05-14 21:43:31.150: i/system.out(878): texteditvalue 05-14 21:43:31.150: d/androidruntime(878): shutting down vm 05-14 21:43:31.150: w/dalvikvm(878): threadid=1: thread exiting uncaught exception (group=0x40a71930) 05-14 21:43:31.310: e/androidruntime(878): fatal exception: main 05-14 21:43:31.310: e/androidruntime(878): java.lang.illegalstateexception: not execute method of activity 05-14 21:43:31.310: e/androidruntime(878): @ android.view.view$1.onclick(view.java:3599) 05-14 21:43:31.310: e/androidruntime(878): @ android.view.view.performclick(view.java:4204) 05-14 21:43:31.310: e/androidruntime(878): @ android.view.view$performclick.run(view.java:17355) 05-14 21:43:31.310: e/androidruntime(878): @ android.os.handler.handlecallback(handler.java:725) 05-14 21:43:31.310: e/androidruntime(878): @ android.os.handler.dispatchmessage(handler.java:92) 05-14 21:43:31.310: e/androidruntime(878): @ android.os.looper.loop(looper.java:137) 05-14 21:43:31.310: e/androidruntime(878): @ android.app.activitythread.main(activitythread.java:5041) 05-14 21:43:31.310: e/androidruntime(878): @ java.lang.reflect.method.invokenative(native method) 05-14 21:43:31.310: e/androidruntime(878): @ java.lang.reflect.method.invoke(method.java:511) 05-14 21:43:31.310: e/androidruntime(878): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-14 21:43:31.310: e/androidruntime(878): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-14 21:43:31.310: e/androidruntime(878): @ dalvik.system.nativestart.main(native method) 05-14 21:43:31.310: e/androidruntime(878): caused by: java.lang.reflect.invocationtargetexception 05-14 21:43:31.310: e/androidruntime(878): @ java.lang.reflect.method.invokenative(native method) 05-14 21:43:31.310: e/androidruntime(878): @ java.lang.reflect.method.invoke(method.java:511) 05-14 21:43:31.310: e/androidruntime(878): @ android.view.view$1.onclick(view.java:3594) 05-14 21:43:31.310: e/androidruntime(878): ... 11 more 05-14 21:43:31.310: e/androidruntime(878): caused by: java.lang.nullpointerexception 05-14 21:43:31.310: e/androidruntime(878): @ com.safm.userprofile.update(userprofile.java:31) 05-14 21:43:31.310: e/androidruntime(878): @ com.safm.userprofile.updateinfo(updateprofile.java:33) 05-14 21:43:31.310: e/androidruntime(878): ... 14 more
note: i've clean project , defined textview in profileuser class , textedit in updateprofile outside oncreate method doesn't work.
thanks.
activities in android separate containers, in no way allowed directly touch eachother. because application stack behind android 'freezes' activities when no longer on foreground, , swapped hard storage save on ram @ operating system's discretion.
your userprofile
encapsulation should reload data after updateprofile
activity closed, or request communication proper way via startactivityforresult.
your current implementation technically incorrect each perspective, since you're hardcoding updateprofile
activity called userprofile
activity. if day introduce new menu option call directly home screen? activities separate, , should not make such assumptions.
Comments
Post a Comment