简体   繁体   中英

Error when passing data from fragment to activity to a second activity

I sort of figured out how to send the data from a fragment -> activity -> a second fragment. However, when the second fragment tries to pull the data, it runs into a null error. The code that first sends the data is shown below:

Intent i = new Intent(getActivity(), scoring_two.class);
i.putExtra("Cv",Cv);
dicstr_twotank_frag.this.startActivity(i);

The code for the scoring_two activity is below:

package edu.UDayton.www;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

//This is simply the placeholder activity for allowing the fragment to attach to something.
//Fragements deal with screen rotations more easily than activities, which is why all the 
//code is the fragment scoring.  But fragments can't exist by themselves so there needs to
//be a dummy activity to hold them.
public class scoring_two extends FragmentActivity{


@Override
protected void onSaveInstanceState(Bundle saveInstanceState) {
    super.onSaveInstanceState(saveInstanceState);
}
@Override
protected void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    setContentView(R.layout.rel5);
    Intent intent = getIntent();
    Double Cv = intent.getDoubleExtra("Cv", 1.0);
    Fragment scoring = new Fragment();
    Bundle bundle = new Bundle();
    bundle.putDouble("Cv", Cv);
    scoring.setArguments(bundle);
  }
}

And the scoring fragment:

package edu.UDayton.www;
public class scoring extends Fragment {
  public TextView textOverallScore;
  Bundle bundle;
  Double Cv;
  //Button declarations
  Button main;
  //This creates a view that the fragment will use to obtain the actual layout for the activity
public View rootView;
  //This is the beginning of activity initialization.  Since this is an Android fragment, the first
  //step is to attach it to an actual activity.  The activity in this case is essentially a placeholder.
  //All of the real work of this system comes from this Android fragment
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
  }
//This code preserves certain values if you exit the app and come back to it
@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    //This retains all of the data in the app upon screen rotation.  Normally the activity is destroyed and
    //re-created upon rotation.  This prevents this from happening.
    setRetainInstance(true);
}
//These lines obtain the layout view "scoring" to use for the fragment.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.scoring, null);
    //Identify the text that needs to change
    textOverallScore=(TextView)rootView.findViewById(R.id.textOverallScore);
    //Receives data
    bundle = getArguments();
    Cv = bundle.getDouble("Cv", 1.0);
    textOverallScore.setText(Cv+"");
    //Identifies the button
    main = (Button) rootView.findViewById(R.id.buttonMain);
    //Sends user back to main screen
    main.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), systems.class);
            scoring.this.startActivity(i);
        }
    });
    return rootView;
}

}

I believe the error occurs at the following line.

   Cv = bundle.getDouble("Cv", 1.0);

If I comment out the line, and just have Cv = 0.0 or something else, it makes the proper change to the output file. Any help would be appreciated, thanks!

After the help from @dhun, I have updated the code for the scoring fragment to the following:

public class scoring extends Fragment {
public TextView textOverallScore;
//Button declarations
Button main;
//This creates a view that the fragment will use to obtain the actual layout for the activity
public View rootView;
//This is the beginning of activity initialization.  Since this is an Android fragment, the first
//step is to attach it to an actual activity.  The activity in this case is essentially a placeholder.
//All of the real work of this system comes from this Android fragment
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);}
public Bundle getBundle() {
    Bundle bundle = new Bundle();
    return bundle;
}
//This code preserves certain values if you exit the app and come back to it
@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    //This retains all of the data in the app upon screen rotation.  Normally the activity is destroyed and
    //re-created upon rotation.  This prevents this from happening.
    setRetainInstance(true);
}
//These lines obtain the layout view "scoring" to use for the fragment.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.scoring, null);
    //Identify the text that needs to change
    textOverallScore=(TextView)rootView.findViewById(R.id.textOverallScore);
    //Receives data
    Double Cv = getBundle().getDouble("Cv",1.36);
    textOverallScore.setText(Cv + "");
    //Identifies the button
    main = (Button) rootView.findViewById(R.id.buttonMain);
    //Sends user back to main screen
    main.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), systems.class);
            scoring.this.startActivity(i);
        }
    });


return rootView;
}

}

And now it no longer crashes, but it only passes the final default value through, it does not receive the Cv value (or it cannot find it).

I think the bundle you are getting in scoring fragment's onCreatView() by

  bundle = getArguments();

returns null bundle and that is why you are getting null pointer exception at the line you have mentioned. Try implementing callback interface to send data back and forth between the activity and fragment. Let me give you a good example. You should refer to this answer posted here and make your code like that and try.

https://stackoverflow.com/a/16578326/4150528

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