简体   繁体   中英

Why do I get a null pointer exception while passing the data from activity to fragment?

Here is my main activity where I want to pass the cityname's text to my TabFragment1 .I am getting null pointer exception.Please help me out in resolving the issue.Even transaction commit () is not solving the problem,Is anything wrong with the code.

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);



}
}

Below is my TabFragment1.java file

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;
}
}

Attached here with tab_fragment1.xml file

<?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>

I am getting the followinng error:

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)....

It seems you are trying to pass a value to the TabFragment1 instance. The best way to do this is by using newInstance()

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

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

  return myFragment;
}

Write this method in your TabFragment1 class and use this when trying to pass value to a Fragment.

Edit: I suggested this because you have to call "setArguments()" before the Fragment is attached to the activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM