[英]Send data from one activity to a fragment on a second activity
我有一個主要的活動,其中包含一個導航抽屜和幾個片段,另一個活動是收集一些我存儲在稱為“端點”的字符串中的數據。 我正在嘗試將字符串從輔助活動發送到主要活動的片段之一:
次要活動(通過意圖向“主要”活動發送“端點”字符串):
Intent intent = new Intent(Secondary.this,Main.class);
intent.putExtra("endpoint",endpoint);
startActivity(intent);
主要活動(從次要活動獲取字符串):
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: ...
在Fragment類上,我試圖在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;
}
這是我得到的:
E / AndroidRuntime致命故障:主進程:com.Main.app,PID:16681 java.lang.RuntimeException:無法啟動活動ComponentInfo {com.Main.app/com.Main.app.Main}:android.view。 InflateException:二進制XML文件第30行:錯誤膨脹類片段原因:android.view.InflateException:二進制XML文件第30行:錯誤將類片段膨脹為android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) android.view.LayoutInflater.rInflate(LayoutInflater.java:758)的android.view.LayoutInflater.rInflate(LayoutInflater.java:755)android.view.LayoutInflater的android.view.LayoutInflater.inflate(LayoutInflater.java:492)的。 android.view.LayoutInflater.inflate(LayoutInflater.java:353)位於com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)位於android.app.Activity處的inflate(LayoutInflater.java:397) .setContentView(Activity.java:1929)在com.Main.app.Main.onCreate(Main.java:42)在android.app.Activity.performCreate(Activity.java:5) 231)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)在android。 android.os.Handler.dispatchMessage(Handler.java:102)上的app.ActivityThread.access $ 800(ActivityThread.java:144)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205)在android.os。 android.app.ActivityThread.main(ActivityThread.java:5146)處的Looper.loop(Looper.java:136)java.lang.reflect.Method.invoke(java.lang.reflect.Method.invokeNative(Native Method)處的Looper.loop(Looper.java:136) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)的Method.java:515)dalvik.system處com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)的MethodAndArgsCaller.run(ZygoteInit.java:566) .NativeStart.main(本機方法)
**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)
在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)在android.view.LayoutInflater.rInflate(LayoutInflater.java:758)在android.view.LayoutInflater.inflate(LayoutInflater.java:492)在android.view。 android.app上的LayoutInflater.inflate(LayoutInflater.java:397)android.view.LayoutInflater.inflate(LayoutInflater.java:353)com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)android.app在com.Main.app.Main.onCreate(Main.java:42)在android.app.Activity.performCreate(Activity.java:5231)處的.Activity.setContentView(Activity.java:1929)在android.app.Instrumentation處。 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)的callActivityOnCreate(Instrumentation.java:1087)android.app.ActivityThread.access $ 800(ActivityThrea) d.java:144)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205)android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java :136),位於android.app.ActivityThread.main(ActivityThread.java:5146),位於java.lang.reflect.Method.invokeNative(Native Method),位於java.lang.reflect.Method.invoke(Method.java:515), com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)在dalvik.system.NativeStart.main(本機方法) )
如何固定它並將字符串從一個活動發送到另一個活動的片段?
這是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>
好的,這就是您的Constants類的外觀。
public class Constants {
public static Constants constants=null;
public static Constants shared(){
if (constants==null){
constants = new Constants();
}
return constants;
}
public static String endpoint;
public static String getEndpoint() {
return endpoint;
}
public static void setEndpoint(String endpoint) {
Constants.endpoint = endpoint;
}
}
現在,在第二個活動中,執行以下操作:
Constants.shared().setEndpoint(endpoint);
Intent intent = new Intent(Secondary.this,Main.class);
startActivity(intent);
然后在您的Fragment類中,執行以下操作:
@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);
return rootView;
}
如果您的主要活動是委派數據並將工作分解成片段的中心點,那么我也將信息存儲在那里。 如果您在Main活動中有一個成員變量來存儲端點,則每個片段都可以通過以下方式訪問它:
((Main) getActivity()).setEndpoint(endpoint)
((Main) getActivity()).getEndpoint()
這樣,您的主要活動就是控制器。
您甚至不必使用intent.putExtra即可完成此操作。 您可以在項目中簡單地引入一些靜態變量。
創建一個稱為“常量”的類。
在那個班上,做一個變量
public static String endpoint;
然后,在您的輔助活動中,而不是putExtra,只需執行以下操作:
Constants.endpoint = "your value";
然后,只需在片段中執行以下操作:
TextView textView = (TextView) rootView.findViewById(R.id.overviewTV);
textView.setText(Constants.endpoint);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.