I use the NavigateDrawer Activity and I need to transfer the data from the Activity to the Fragment. When trying to get data in a Fragment in the getArguments () method. GetString ("data"); - I get java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString (java.lang.String)' on a null object reference

Activity.class

public class MainActivity extends Activity implements onEventListener,...{ private static FragmentManager fragmentManagerPass; private DrawerLayout myDrawerLayout; private ListView myDrawerList; private android.support.v7.app.ActionBarDrawerToggle myDrawerToggle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_nav_draw); /* ... */ } /* ... */ @Override public void mySomeEvent(String inputPair) { Toast.makeText(this, "Activity receive(adopt) " + input_mySome, Toast.LENGTH_SHORT).show(); fragmentManagerPass = getFragmentManager(); MyFragment argumentFragment = new MyFragment(); Bundle data = new Bundle(); data.putString("data", "This is Argument Fragment fromActivity"); argumentFragment.setArguments(data); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick( AdapterView<?> parent, View view, int position, long id ) { displayView(position); } } private void displayView(int position) { android.app.Fragment fragment = null; switch (position) { case 0: fragment = new MainList(); break; case 1: fragment = new ScreenTwo(); break; default: break; } if (fragment != null) { android.app.FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment).commit(); myDrawerList.setItemChecked(position, true); myDrawerList.setSelection(position); setTitle(viewsNames[position]); myDrawerLayout.closeDrawer(myDrawerList); } else { // error in creating fragment Log.e("MainActivity", "Error in creating fragment"); } } 

Fragragment.class

 public class MyFragment extends Fragment { final String LOG_TAG = "myLogs"; Button buttonOne,buttonTwo ; TextView txtOne,txtTwo,txtThree,txtFour; int myInt; String myStr; // private OnFragmentInteractionListener mListener; public MyFragment() { // Required empty public constructor } public interface onEventListener { public void mySomeEvent(String s); } onEventListener myEventListener; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_converter,container,false); txtOne = (TextView) view.findViewById(R.id.textView7); Button buttonOne = (Button) view.findViewById(R.id.btn2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do something Toast.makeText(getActivity(),"button",Toast.LENGTH_SHORT).show(); txtFour.setText("hello1"); } }); //receive data from activity //Get Argument that passed from activity in "data" key value String getArgument = getArguments().getString("data");//java.lang.NullPointerException txtOne.setText(getArgument);//set string over textview Log.d(LOG_TAG,"Bundle Fragmnet is "+getArgument); Log.d(LOG_TAG, "Fragment2 onCreateView"); return view; } } 
  • You have something completely incomprehensible by the code of what you select and what you expect from your actions. You give arguments to your one fragment, but you do not display this fragment. And others, without arguments, do not display. Plus the code is badly formatted. We will not be able to help you that way - YuriySPb
  • the code just cut out a lot there too much left as it seems to me the most basic - mtb

1 answer 1

MyFragment class update

In order to transfer data to the Fragment, add in MyFragment.class:

 private static String EXTRA_STRING = "my_fragment_extra_string"; 

Then, add a static method

 public static MyFragment newInstance(String message) { Bundle args = new Bundle(); args.putString(EXTRA_STRING, message); MyFragment fragment = new MyFragment (); fragment.setArguments(args); return fragment; } 

To get the data in the fragment, add the following code to the onCreateView method:

 String myMessage = getArguments().getString(EXTRA_AMOUNT); 

Creating Fragment Objects

Now the creation of fragments will not occur through the fragment constructor , but through the newInstance method , into which it will be possible to pass a String (or something else, everything depends on the signature of the newInstance method).

  • I get the same application crash, in View onCreateView on the String line myMessage = getArguments (). getString (EXTRA_STRING); --- java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString (java.lang.String)' on a null object reference - it seems to me that this is somehow connected NavigationDrawer when this fragment is loaded because even a null check does not save from departure - mtb
  • Do you create a copy of your fragment? Show this line. - tse
  • fragmentManagerPass = getFragmentManager (); MyFragment argumentFragment = new MyFragment (); Bundle data = new Bundle (); data.putString ("data", "This is Argument Fragment fromActivity"); argumentFragment.setArguments (data); - mtb
  • Why do you ask for advice, and then acts in its own way? I wrote that creating a fragment should be done not through their constructor, but through the static newInstance method - Valery Ponomarenko