Android startActivityForResult() -
i started learning android programming have problem starting activity result(using onactivityresult(), setresult()).
first, there 3 activity - main, register, subactivity(but question regard 2 activity - main, register). registered of them 'androidmanifest.xml' this:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wakwakwak.iwak.myapp" > <uses-permission android:name="android.permission.vibrate"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".main" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".subactivity" android:label="here subworld!"> </activity> <activity android:name=".register" android:label="register"> </activity> </application> </manifest>
as see, 3 activities registered.
this main activity.
and if touch 'reg' button, move 'register' activity.
if reg touched, register activity returns information main activity , main activity saves arraylist.
code below main activity(main.java).
package com.wakwakwak.iwak.myapp; import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import android.os.vibrator; import android.view.view; import android.widget.edittext; import android.widget.toast; import java.util.arraylist; public class main extends activity { private static arraylist<person> users; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); vibrator vibrator = (vibrator)getsystemservice(context.vibrator_service); vibrator.vibrate(300); } public void onclick(view v){ edittext edid = (edittext)findviewbyid(r.id.edit_id); edittext edpw = (edittext)findviewbyid(r.id.edit_pw); vibrator vibrator = (vibrator)getsystemservice(context.vibrator_service); switch(v.getid()){ case r.id.btn_login: vibrator.vibrate(300); person person = new person(edid.gettext().tostring(),edpw.gettext().tostring()); intent intent1 = new intent(this, subactivity.class); intent1.putextra("person",person); startactivity(intent1); break; case r.id.btn_register: vibrator.vibrate(300); intent intent2 = new intent(this,register.class); startactivityforresult(intent2, 0); break; } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch(requestcode) { case 0: person person = (person)data.getserializableextra("person"); users.add(person); toast.maketext(this,person.tostring(),toast.length_short).show(); break; } } }
and code below register activity(register.java)
package com.wakwakwak.iwak.myapp; 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 register extends activity { @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); } public void onclick(view view) { edittext editid = (edittext)findviewbyid(r.id.edit_id); edittext editpw = (edittext)findviewbyid(r.id.edit_pw); edittext editpwagain = (edittext)findviewbyid(r.id.edit_pwagain); switch(view.getid()){ case r.id.btn_register: if(!editpw.gettext().tostring().equals(editpwagain.gettext().tostring())){ editpw.settext(""); editpwagain.settext(""); toast.maketext(this,"passwords not equal each other",toast.length_short).show(); } else{ person person = new person(editid.gettext().tostring(),editpw.gettext().tostring()); intent intent2 = new intent(); intent2.putextra("person",person); setresult(0,intent2); finish(); } break; } } }
(person class have member 'id' , 'pw'. seems there's no problem on person class)
it seems register activity works - shows error toast when each password different, when both of them same(when passing main activity), error occurs: 'myapp stopped' can't know problem!! please let me know wrong :(
when both of them same(when passing main activity), error occurs: 'myapp stopped'
probably getting npe
exception users
arraylist null
when calling add
method in onactivityresult
. add following line in oncreate
method:
users=new arraylist<person>();
Comments
Post a Comment