Monday, April 13, 2015

Android : 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!


No comments:

Post a Comment