Adding a fragment in android on button click and adding data to it simultaneously -
i creating android app in have main activity has 2 fragments, fragup , fragdown. fragup has 2 edit text fields , button , fragdown has 2 text view fields.
initially main activity shows fragup , asks user input fields, when user clicks button onclick method triggered , fragdown created below fragup , shows 2 fields entered user.
summary-
fragments- fragup , fragdown
main activity- main activity
main activity has 2 linear layouts
1.ly1 fragup
2.ly2 fragdown
note- fragments added dynamically in main java file , not declared in main.xml file.
this main activity code
public class mainactivity extends appcompatactivity implements fragup.fraginterface { fragmenttransaction ft,ft1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); ft=getfragmentmanager().begintransaction(); fragup fu=new fragup(); ft.add(r.id.ly1,fu); //fragment fragup added dynamically. ft.commit(); } public void set(string a,string b){ ft1=getfragmentmanager().begintransaction(); fragdown ob=new fragdown(); ft1.add(r.id.ly2,ob); ft1.commit(); fragdown fd=(fragdown)getfragmentmanager().findfragmentbyid(r.id.ly2); fd.seta(a, b); }
this fragup java code-
public class fragup extends fragment { view v; public interface fraginterface{ public void set(string a,string b); } fraginterface ob; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { v=inflater.inflate(r.layout.fragup,container,false); button b=(button)v.findviewbyid(r.id.button1); b.setonclicklistener( new onclicklistener(){ public void onclick(view v){ store(); }}); return v; } public void onattach(activity activity){ super.onattach(activity); try{ ob=(fraginterface)activity; } catch(classcastexception e){ throw new classcastexception(); } } public void store(){ main activity edittext et1=(edittext)v.findviewbyid(r.id.edittext1); edittext et2=(edittext)v.findviewbyid(r.id.edittext2); ob.set(et1.gettext().tostring(),et2.gettext().tostring()); //interface implemented share data } }
this fragdown java code-
public class fragdown extends fragment { view v; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { v=inflater.inflate(r.layout.fragdown,container,false); return v; } public void seta(string a,string b){ textview t1=(textview)v.findviewbyid(r.id.textview1); textview t2=(textview)v.findviewbyid(r.id.textview2); t1.settext(a); t2.settext(b); } }
when run app , press button app crashes , logcat says-
08-05 09:37:02.198: e/androidruntime(2866): java.lang.nullpointerexception: attempt invoke virtual method 'void com.example.fragment.fragdown.seta(java.lang.string, java.lang.string)' on null object reference 08-05 09:37:02.198: e/androidruntime(2866): @ com.example.fragment.second.set(second.java:30) 08-05 09:37:02.198: e/androidruntime(2866): @ com.example.fragment.fragup.store(fragup.java:52) 08-05 09:37:02.198: e/androidruntime(2866): @ com.example.fragment.fragup$1.onclick(fragup.java:25)
if can me great. spent hours debugging can't find went wrong. please help.
why don't pass values fragment arguments? check "layout" section on guide: http://developer.android.com/reference/android/app/fragment.html
here's excerpt guide you:
public static detailsfragment newinstance(int index) { detailsfragment f = new detailsfragment(); // supply index input argument. bundle args = new bundle(); args.putint("index", index); f.setarguments(args); return f; }
then when setting fragment:
// prepare args bundle arguments = new bundle(); arguments.putstring(key, value); // init fragment , pass args fragment = new detailsfragment(); fragment.setarguments(arguments); // commit fragment manager, // inflating view getsupportfragmentmanager().begintransaction() .add(r.id.your_id, fragment).commit();
you can access bundled arguments on fragment's oncreate
method.
addressing code specifically, should:
mainactivity.set
, pass args fragment:public void set(string a,string b) { ft1 = getfragmentmanager().begintransaction(); fragdown ob = new fragdown(); // add args. practice // declare keys public static final // variables, accessing them accordingly. here // i'm putting string, should // avoid that. bundle args = new bundle(); args.putstring("key_a", a); args.putstring("key_b", b); ob.setarguments(args); ft1.add(r.id.ly2, ob); ft1.commit(); }
add
fragdown.oncreate
method:private string = ""; private string b = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.a = getarguments().getstring("key_a"); this.b = getarguments().getstring("key_b"); }
call
seta
ononcreateview
:@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { v = inflater.inflate(r.layout.fragdown, container, false); seta(this.a, this.b) return v; }
this due fragment's lifecycle: http://developer.android.com/reference/android/app/fragment.html#lifecycle
now, bit of advice:
- if you're serious android development, go read links posted (from top bottom) until understand them. sorry, there's no way around them.
- the "show me code" kind of questions considered rude , prone downvoted or hateful comments. stackoverflow working towards "be nice" campaign, , that's 1 of main reasons coped , followed up.
- asking question in stackoverflow should last resource, after you've exhausted every other option , have gone through docs, tutorials, source code, github issues , pull requests, , on.
- take advice grain of salt :)
Comments
Post a Comment