java - getArguments() is returning null -
i'm having problem using fragments. indeed, i'm trying pass arguments fragment activity. problem getarguments()
call returns null. here code:
albumuser.java :
public class albumuser extends appcompatactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_album_user); string albumid = getintent().getstringextra("albumid"); string albumname = getintent().getstringextra("albumname"); string albumauthor = getintent().getstringextra("albumauthor"); string coverurl = getintent().getstringextra("coverurl"); getfragmentmanager().begintransaction().replace(r.id.fragmentalbum, album.newinstance(albumid,albumname,albumauthor,coverurl)).commit(); } }
album.java :
public class album extends fragment { private string albumid; private string albumname; private string albumauthor; private string coverurl; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.oncreateview(inflater, container, savedinstancestate); view = inflater.inflate(r.layout.activity_album, container, false); bundle args = getarguments(); albumid = args.getstring("albumid"); albumname = args.getstring("albumname"); albumauthor = args.getstring("albumauthor"); coverurl = args.getstring("coverurl"); return view; ) public static album newinstance(final string albumid, final string albumname, final string albumauthor, final string coverurl) { final album album = new album(); final bundle bundle = new bundle(); bundle.putstring("albumid", albumid); bundle.putstring("albumname", albumname); bundle.putstring("albumauthor", albumauthor); bundle.putstring("coverurl", coverurl); album.setarguments(bundle); return album; } }
activity_album.xml :
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:context=".mainactivity"> <framelayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <fragment android:id="@+id/fragmentalbum" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.example.jeff.facebookphotocast.album" /> <include layout="@layout/toolbar"/> </framelayout> </android.support.v4.widget.drawerlayout>
please me, i'm dying ^^ :)
the layout xml
<fragment android:id="@+id/fragmentalbum" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.example.jeff.facebookphotocast.album" />
instantiates album
fragment without arguments. that's why getarguments()
returns null.
if want supply arguments fragment, replace part of layout e.g. empty framelayout
container, , use fragmenttransaction#add()
put fragment-with-args in container.
Comments
Post a Comment