-1

I've been trying to send a string of data from my activity to a fragment using bundle but when I try to access it, it gives a nullpointerexception.

The oncreate method in the activity looks like this

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chart);

    // Setup toolbar and title
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    readDb();
    printChart();
    printTop10();

    Bundle bundle = new Bundle();
    bundle.putString("edttext", myString);
    // set Fragmentclass Arguments
    ToolbarFragment fragobj = new ToolbarFragment();
    fragobj.setArguments(bundle);

}

and the fragment on the onCreateView looks like this

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

    String strtext = getArguments().getString("edttext");
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_toolbar, container, false);
}

any help to get rid of the nullpointer would be appreciated, I'm trying to send data to the fragment so it can use the share button and share the data

Kevin
  • 75
  • 6
  • You're not transacting `fragobj` anywhere. How exactly are you loading `ToolbarFragment` into the `Activity`? – Mike M. Nov 19 '17 at 00:07

1 Answers1

0

It seems that the getting part of the "edttext" key is causing null because it is being triggered when no value was actually assigned to it, so I guess getting the "edttext" key with a default value could avoid the null.

Do like that in the oncreateview of the fragment

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

//in this line add these "" as a default value to avoid null.

String strtext = getArguments().getString("edttext","");

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_toolbar, container, false);
  }

EDIT

Forget the previous answer and do this:

delete this:

         Bundle bundle = new Bundle();
bundle.putString("edttext", myString);
// set Fragmentclass Arguments
ToolbarFragment fragobj = new ToolbarFragment();
fragobj.setArguments(bundle);

and lets say your activity name is (MyActivity), then do this under oncreate

       private String myString="whatever";

       @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_chart);

      // Setup toolbar and title
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      readDb();
      printChart();
      printTop10();


       }


    //add this method (this method will send the myString to fragment)
    public String getmyString(){


   return myString;
   }

now go to your fragment and remove this from oncreateview

    String strtext = getArguments().getString("edttext");

and do like this in fragment

         private String strtext="";

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

 //now get the string like that 

  strtext=((MyActivity)getActivity).getmyString();

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_toolbar, container, false);

}

Update

1) make sure that the (items) list is not null. You should initialize it before populating it (to detect the problem it might be better to show how you populate your items list).

2) try this in getmystring() method :

         public String getmyString(){

         for(int i=0 ; i<items ; i++){

          myString=items.get(i);
         }

         return myString;
        }

just don't forget to initialize your items list in onCreate before you populate with anything.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • It crashed with it saying Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String, java.lang.String)' on a null object reference adding the "" after edttext – Kevin Nov 19 '17 at 00:17
  • are you passing (mystring) as empty or it has a value? – Hasan Bou Taam Nov 19 '17 at 00:21
  • paste the code for (mystring) how do you set a value for it. – Hasan Bou Taam Nov 19 '17 at 00:22
  • i made it global in my activity : private String myString = "Sending String"; – Kevin Nov 19 '17 at 00:27
  • check the EDIT and hope it helps. – Hasan Bou Taam Nov 19 '17 at 00:49
  • I tried that method but one problem I get is that when I tried to change the value, it doesn't work. Since I don't want the exact value, I'm trying to populate a string using a concat so if i try to use to change the values through the share activity, it won't change – Kevin Nov 19 '17 at 00:54
  • just paste the code that you change the string with – Hasan Bou Taam Nov 19 '17 at 00:55
  • It crashes when I try to do this at the getData method to pass the string to the fragment `public String getMyData() { for (int i = 0; i < items.length; i++) { myString += items[i]; } return myString; }` It gives a nullpointer error even though list should be populated from the onCreate, then I just did what you did on th e onCreateView – Kevin Nov 19 '17 at 02:47
  • check the UPDATE, and it would be better to edit your question and show how you populate the items list. – Hasan Bou Taam Nov 19 '17 at 03:56