Enviar dados de uma atividade para um fragmento em uma segunda atividade
Eu tenho uma atividade principal com uma gaveta de navegação e vários fragmentos e uma atividade secundária que coleta alguns dados que eu armazeno em uma sequência chamada "pontos finais". Estou tentando enviar a string da atividade secundária para um dos fragmentos da atividade principal:
Atividade Secundária (enviando a string "endpoint" via intenção para a Atividade Principal):
Intent intent = new Intent(Secondary.this,Main.class);
intent.putExtra("endpoint",endpoint);
startActivity(intent);
Atividade principal (obtendo string da atividade secundária):
public void onNavigationDrawerItemSelected(int position) {
// update the main content by 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: ...
E na classe Fragment, estou tentando exibir os dados em um 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);
return rootView;
}
Isto é o que eu recebo:
E / AndroidRuntime EX EXCEÇÃO FATAL: processo principal: com.Main.app, PID: 16681 java.lang.RuntimeException: Não foi possível iniciar a atividade ComponentInfo {com.Main.app/com.Main.app.Main}: android.view. InflateException: Linha de arquivo XML binário # 30: Erro ao inflar o fragmento de classeCausada por: android.view.InflateException: Linha de arquivos XML binária # 30: Erro ao inflar o fragmento de classe em android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:713) em android.view.LayoutInflater.rInflate (LayoutInflater.java:755) em android.view.LayoutInflater.rInflate (LayoutInflater.java:758) em android.view. LayoutInflater.inflate (LayoutInflater.java:492) em android.view.LayoutInflater.inflate (LayoutInflater.java:397) em android.view.LayoutInflater.inflate (LayoutInflater.java:353) em com.android.internal.policy.impl .PhoneWindow.setContentView (PhoneWindow.java:343) em android.app.Activity.setContentView (Activity.java:1929) em com.Main.app.Main.onCreate (Main.java:42) em android.app.Activity. performCreate (Activity.java:5231) em android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1087) em android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2169) em android.app.ActivityThread.handleLaunchActivity (ActivityThread.handleLaunchActivity (ActivityThread.handleLaunchActivity)) : 2271) em android.app.ActivityThread.access $ 800 (ActivityThread.java:144) em android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1205) em android.os.Handler.dispatchMessage (Handler.java:102) em android.os.Looper.loop (Looper.java:136) no android. app.ActivityThread.main (ActivityThread.java:5146) em java.lang.reflect.Method.invokeNative (método nativo) em java.lang.reflect.Method.invoke (Method.java:515) em com.android.internal. os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:732) em com.android.internal.os.ZygoteInit.main (ZygoteInit.java:566) em dalvik.system.NativeStart.main (método nativo)
**Caused by: java.lang.NullPointerException**
at com.Main.app.Main.onNavigationDrawerItemSelected(Main.java:65)
at com.Main.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:205)
at com.Main.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:79)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
at android.app.Activity.onCreateView(Activity.java:4786)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
em android.view.LayoutInflater.rInflate (LayoutInflater.java:755) em android.view.LayoutInflater.rInflate (LayoutInflater.java:758) em android.view.LayoutInflater.inflate (LayoutInflater.java:492) em android.view. LayoutInflater.inflate (LayoutInflater.java:397) em android.view.LayoutInflater.inflate (LayoutInflater.java:353) em com.android.internal.policy.impl.PhoneWindow.setContentView (PhoneWindow.java:343) em android.app .Activity.setContentView (Activity.java:1929) em com.Main.app.Main.onCreate (Main.java:42) em android.app.Activity.performCreate (Activity.java:5231) em android.app.Instrumentation. callActivityOnCreate (Instrumentation.java:1087) em android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2169) no android. app.ActivityThread.handleLaunchActivity (ActivityThread.java:2271) em android.app.ActivityThread.access $ 800 (ActivityThread.java:144) em android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1205) em android.os. Handler.dispatchMessage (Handler.java:102) em android.os.Looper.loop (Looper.java:136) em android.app.ActivityThread.main (ActivityThread.java:5146) em java.lang.reflect.Method.invokeNative (Método nativo) em java.lang.reflect.Method.invoke (Method.java:515) em com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:732) em com.android.internal.os .ZygoteInit.main (ZygoteInit.java:566) em dalvik.system.NativeStart.main (método nativo)
Como posso corrigi-lo e enviar a sequência de uma atividade para um fragmento em outra atividade?
Aqui está o XML:
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full 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">
<!-- As the main content view, the view below consumes the 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 to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the 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>