这是我的主要活动,我想将城市名称的文本传递给我
TabFragment1。我正在获取空指针异常。请帮助我解决问题。即使事务commit()也不能解决问题,代码有什么问题。

public class MainActivity extends AppCompatActivity {
EditText cityname;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    cityname = findViewById(R.id.cityname);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    String cn = cityname.getText().toString();

    Bundle bundle = new Bundle();
    bundle.putString("city",cn);
    TabFragment1 fobj = new TabFragment1();
    fobj.setArguments(bundle);



}
}


以下是我的TabFragment1.java文件

public class TabFragment1 extends Fragment {
TextView tx;
String s="Hello";
Intent it;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.tab_fragment_1 , container, false);
    tx=(TextView)view.findViewById(R.id.textView1);

    s=this.getArguments().getString("city");
    tx.setText(s);

     return view;
}
}


随附tab_fragment1.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">


<TextView
    android:id="@+id/textView1"
    android:layout_width="161dp"
    android:layout_height="66dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="TextView" />
</RelativeLayout>


我收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
    at com.example.ps.trial1.TabFragment1.onCreateView(TabFragment1.java:19)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)....

最佳答案

似乎您正在尝试将值传递给TabFragment1实例。最好的方法是使用newInstance()

public static TabFragment1 newInstance(String someCity) {
  TabFragment1 myFragment = new TabFragment1();

  Bundle args = new Bundle();
  args.putString("city", someCity);
  myFragment.setArguments(args);

  return myFragment;
}


在您的TabFragment1类中编写此方法,并在尝试将值传递给Fragment时使用此方法。

编辑:我建议这样做,因为您必须在将片段附加到活动之前调用“ setArguments()”

08-05 10:46