danilojara123
danilojara123

Reputation: 398

Passing data between Fragments in Android

I know that many have answered this question, but I still cannot get mine to work. In StartWorkout class I get data from a chronometer, and since that data is in milliseconds I use a method that I coded called showElapsedtime(). to convert those milliseconds into seconds,minutes, and hours. I want to pass that data to another class called workouts, but for some reason it is not working.I am not getting any data because I get a NUllPOINTEREXECPTION

Here is my code.

StartWorkout class

    package com.example.d_jara.apprunners;

    public class StartWorkout extends Fragment{


    Button button;
    Button button2;
    Chronometer mChronometer;
    private int hours = 0;
    private int minutes = 0;
    private  int seconds=0;



    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.lay_startworkout, container, false);




        mChronometer = (Chronometer) rootView.findViewById(R.id.chronometer);
       // mChronometer.setFormat("H:MM:SS");
        button = (Button) rootView.findViewById(R.id.mystart);
        button.setOnClickListener(mStartListener);
        mChronometer.setTextSize(70);





        button2 = (Button) rootView.findViewById(R.id.finishWork);
        //button2.setOnClickListener(mFinishListener);
        //finish workout button
         rootView.findViewById(R.id.finishWork).setVisibility(rootView.GONE);




      //test code here


        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


                mChronometer.stop();
                button.setVisibility(v.VISIBLE);
                button2.setVisibility(v.GONE);
                Toast toast = Toast.makeText(getActivity().getBaseContext(),"Your workout was saved", Toast.LENGTH_LONG);
                toast.show();
                toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
                showElapsedTime();

                int c= 9;
                Bundle args = new Bundle();
                Fragment newFragment = new workOuts();
                //number of calories
                args.putInt("hours",hours);
                args.putInt("seconds",seconds);
                args.putInt("hours",hours);
                newFragment.setArguments(args);


            }
        });







        return rootView;
    }


        View.OnClickListener mStartListener = new View.OnClickListener() {
        public void onClick(View v) {
            mChronometer.setBase(SystemClock.elapsedRealtime()-0);
            mChronometer.start();
            button.setVisibility(v.GONE);
            button2.setVisibility(v.VISIBLE);


        }
    };





    private void showElapsedTime() {
        long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();

        hours = (int) (elapsedMillis / 3600000);
         minutes = (int) (elapsedMillis - hours * 3600000) / 60000;
         seconds = (int) (elapsedMillis - hours * 3600000 - minutes * 60000) / 1000;






      }



 }

And here is my workouts class

     public class workOuts extends Fragment {




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.lay_workouts, container, false);






            int myseconds = getArguments().getInt("seconds");
            int myhours = getArguments().getInt("hours");
            int myminutes = getArguments().getInt("minutes");


             String se=String.valueOf(myseconds);
            String minu=String.valueOf(myminutes);
           String hours=String.valueOf(myhours);


           TextView sec1 = (TextView) rootView.findViewById(R.id.seconds);
           TextView minute1 = (TextView) rootView.findViewById(R.id.minutes);
           TextView hours1 = (TextView) rootView.findViewById(R.id.minutes);

            sec1.setText(se);
           minute1.setText(minu);
        hours1.setText(hours);













       return  rootView;
    }


}

Upvotes: 0

Views: 78

Answers (1)

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

 Bundle args = new Bundle();
            Fragment newFragment = new workOuts();
            //number of calories
            args.putInt("hours",hours);
            args.putInt("seconds",seconds);
            args.putInt("hours",hours);
            newFragment.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(yourcontainerid, newFragment, "workout");
ft.commit();

Upvotes: 2

Related Questions