我正在尝试在FragmentActivity和Fragment之间传递数据。
在Fragment Activity OnCreate方法中,pid值从另一个活动发送到此片段活动,我将此pid值传递给片段:
Intent i = getIntent();
Bundle extras = i.getExtras();
pid = extras.getString("TAG_PATIENT_ID");
System.out.println("Pid in prescact"+pid);
// Bundle bundle = new Bundle();
/* bundle.putString("TAG_PATIENT_ID",pid);
Intent m = new Intent(PrescAct.this,DocPresc1.class);
m.putExtras(bundle);*/
//Passing pid value to the list fragment
Bundle b = new Bundle();
b.putString("TAG_PATIENT_ID",pid);
Fragment f = new DocPresc1();
f.setArguments(b);
从此fragmentActivity中检索值到片段时,我得到空指针异常。 在片段类的OnCreateView方法中:
Bundle b = getArguments();
pid = b.getString("TAG_PATIENT_ID");
System.out.println("Docpresc"+pid);
这是堆栈跟踪:
06-20 12:44:34.767: E/AndroidRuntime(9394): FATAL EXCEPTION: main
06-20 12:44:34.767: E/AndroidRuntime(9394): java.lang.NullPointerException
06-20 12:44:34.767: E/AndroidRuntime(9394): at com.example.actionbar.DocPresc1.onCreateView(DocPresc1.java:104)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.View.measure(View.java:15479)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.View.measure(View.java:15479)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.View.measure(View.java:15479)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.View.measure(View.java:15479)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-20 12:44:34.767: E/AndroidRuntime(9394): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.View.measure(View.java:15479)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.Choreographer.doFrame(Choreographer.java:525)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.os.Handler.handleCallback(Handler.java:615)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.os.Looper.loop(Looper.java:137)
06-20 12:44:34.767: E/AndroidRuntime(9394): at android.app.ActivityThread.main(ActivityThread.java:4895)
06-20 12:44:34.767: E/AndroidRuntime(9394): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 12:44:34.767: E/AndroidRuntime(9394): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 12:44:34.767: E/AndroidRuntime(9394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
06-20 12:44:34.767: E/AndroidRuntime(9394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
06-20 12:44:34.767: E/AndroidRuntime(9394): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:-1)
将数据从一个活动发送到另一个活动的最简单方法是使用Intent,如下所示:
Intent intent = new Intent(Activity.this, FragmentActivity.class);
intent.putExtra("Key", value);
startActivity(intent);
现在获取接收器FragmentActivity中的键值,如下所示:
Declare variable:
public String key;
将on intent中的值从oncreate方法中获取到变量中:
Intent intent = getIntent();
key = intent.getStringExtra("Key");
现在使用此代码在片段中使用此键值:
String key = ((FragmentActivity)this.getActivity()).key;