我試圖在FragmentActivity和Fragment之間傳遞數據。將數據從活動傳遞到片段時出現空指針異常
在片段活性的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)
參閱此http://計算器。 com/questions/12873635 /傳遞參數從android-fragmentactivity到片段 – Riser
我推薦這個stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android –
請提出爲什麼我在線上得到這個空指針異常: pid = b.getString(「TAG_PATIENT_ID」); –