Tuesday 15 September 2015

android - Send data from one activity to a fragment on a second activity -



android - Send data from one activity to a fragment on a second activity -

i have main activity navigation drawer , several fragments , secondary activity collects info store on string called "endpoints". i'm trying send string secondary activity 1 of fragments on main activity:

secondary activity (sending "endpoint" string via intent main activity):

intent intent = new intent(secondary.this,main.class); intent.putextra("endpoint",endpoint); startactivity(intent);

main activity (getting string secondary activity):

public void onnavigationdraweritemselected(int position) { // update main content replacing fragments fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, placeholderfragment.newinstance(position)) .commit(); switch (position) { case 0: bundle args = new bundle(); args.putstring("endpoint",getintent().getextras().getstring("endpoint")); fragment1 fragment = new fragment1(); fragment1.setarguments(args); fragmentmanager.begintransaction().replace(r.id.container, fragment1.newinstance(position)).commit(); break; case 1: fragmentmanager.begintransaction().replace(r.id.container, fragment2newinstance(position)).commit(); break; case 2: fragmentmanager.begintransaction().replace(r.id.container, fragment3.newinstance(position)).commit(); break; case 3: ...

and on fragment class i'm trying display info on textview:

@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { string endpoints = getactivity().getintent().getstringextra("endpointstr"); view rootview = inflater.inflate(r.layout.fragment_overview, container, false); textview textview = (textview) rootview.findviewbyid(r.id.overviewtv); textview.settext(endpoint); homecoming rootview; }

this get:

e/androidruntime﹕ fatal exception: main process: com.main.app, pid: 16681 java.lang.runtimeexception: unable start activity componentinfo{com.main.app/com.main.app.main}: android.view.inflateexception: binary xml file line #30: error inflating class fragment caused by: android.view.inflateexception: binary xml file line #30: error inflating class fragment @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:713) @ android.view.layoutinflater.rinflate(layoutinflater.java:755) @ android.view.layoutinflater.rinflate(layoutinflater.java:758) @ android.view.layoutinflater.inflate(layoutinflater.java:492) @ android.view.layoutinflater.inflate(layoutinflater.java:397) @ android.view.layoutinflater.inflate(layoutinflater.java:353) @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:343) @ android.app.activity.setcontentview(activity.java:1929) @ com.main.app.main.oncreate(main.java:42) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2169)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2271)             at android.app.activitythread.access$800(activitythread.java:144)             at android.app.activitythread$h.handlemessage(activitythread.java:1205)             at android.os.handler.dispatchmessage(handler.java:102)             at android.os.looper.loop(looper.java:136)             at android.app.activitythread.main(activitythread.java:5146)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:515)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:732)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:566)             at dalvik.system.nativestart.main(native method)

**caused by: java.lang.nullpointerexception** @ com.main.app.main.onnavigationdraweritemselected(main.java:65) @ com.main.app.navigationdrawerfragment.selectitem(navigationdrawerfragment.java:205) @ com.main.app.navigationdrawerfragment.oncreate(navigationdrawerfragment.java:79) @ android.app.fragment.performcreate(fragment.java:1678) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:859) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1040) @ android.app.fragmentmanagerimpl.addfragment(fragmentmanager.java:1142) @ android.app.activity.oncreateview(activity.java:4786) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:689)

            at android.view.layoutinflater.rinflate(layoutinflater.java:755)             at android.view.layoutinflater.rinflate(layoutinflater.java:758)             at android.view.layoutinflater.inflate(layoutinflater.java:492)             at android.view.layoutinflater.inflate(layoutinflater.java:397)             at android.view.layoutinflater.inflate(layoutinflater.java:353)             at com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:343)             at android.app.activity.setcontentview(activity.java:1929)             at com.main.app.main.oncreate(main.java:42)             at android.app.activity.performcreate(activity.java:5231)             at android.app.instrumentation.callactivityoncreate(instrumentation.java:1087)             at android.app.activitythread.performlaunchactivity(activitythread.java:2169)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2271)             at android.app.activitythread.access$800(activitythread.java:144)             at android.app.activitythread$h.handlemessage(activitythread.java:1205)             at android.os.handler.dispatchmessage(handler.java:102)             at android.os.looper.loop(looper.java:136)             at android.app.activitythread.main(activitythread.java:5146)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:515)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:732)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:566)             at dalvik.system.nativestart.main(native method)

how can fixed , send string 1 activity fragment on activity?

here's xml:

<!-- drawerlayout intended used top-level content view using match_parent both width , height consume total space available. --> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true"> <android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" tools:context=".stackerz"> <!-- main content view, view below consumes entire space available using match_parent in both dimensions. --> <framelayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- android:layout_gravity="start" tells drawerlayout treat sliding drawer on left side left-to-right languages , on right side right-to-left languages. if you're not building against api 17 or higher, utilize android:layout_gravity="left" instead. --> <!-- drawer given fixed width in dp , extends total height of container. --> <fragment android:id="@+id/navigation_drawer" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" android:name="com.xxxxxx.app.navigationdrawerfragment" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.drawerlayout> </framelayout>

ok, how constants class should like.

public class constants { public static constants constants=null; public static constants shared(){ if (constants==null){ constants = new constants(); } homecoming constants; } public static string endpoint; public static string getendpoint() { homecoming endpoint; } public static void setendpoint(string endpoint) { constants.endpoint = endpoint; } }

now, in sec activity, this:

constants.shared().setendpoint(endpoint); intent intent = new intent(secondary.this,main.class); startactivity(intent);

and in fragment class, this:

@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { string endpoint = constants.shared().getendpoint; view rootview = inflater.inflate(r.layout.fragment_overview, container, false); textview textview = (textview) rootview.findviewbyid(r.id.overviewtv); textview.settext(endpoint); homecoming rootview; }

android android-intent android-activity android-fragments bundle

No comments:

Post a Comment