使用bundle传递数据时,使用不同的活动从Fragment获取错误

时间:2017-01-02 07:35:06

标签: java android android-fragments nullpointerexception

我正在使用Bundle将数据从MainActivity传递到Fragment,但它在第二个活动中的list_fragments2中提示出JAVA.NULLPOINTER.EXCEPTION . Error is occur when I getArguments()`

MainActivity

list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
Toast.makeText(this, "" + b, Toast.LENGTH_SHORT).show();

list_fragment2

String name = getArguments().getString("test");
ntab2.setText(name);

logcat的

01-02 07:19:52.549 15797-15797/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                      java.lang.NullPointerException
                  at net.simplifiedcoding.androidtablayout.Tab2.list_fragment2.onCreateView(list_fragment2.java:62)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                  at android.support.v4.app.Fragment.performStart(Fragment.java:1983)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
                  at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:926)
                  at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1272)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1630)
                  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)

3 个答案:

答案 0 :(得分:0)

这是解决方案,你必须在list_fragment2中更改一些代码 首先你必须制作bundle然后得到像

这样的String
Bundle bundle = getArguments();
    if (bundle != null) {
    String name = bundle.getString("test");
    ntab2.setText(name);
  }

问题解决。

答案 1 :(得分:0)

更改代码的这一行。

  

String name = getArguments()。getString(“test”);

  

String name = this.getArguments()。getString(“test”);

它会起作用。

答案 2 :(得分:0)

在你的第二个片段上使用像这样

在List_fragment2的onCreateView()方法中调用getArguments():

 public View onCreateView(LayoutInflater inflater,ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //your string
        String test=bundle.getString("test");   
     }
}