在Android中的片段之间传递数据

时间:2014-12-05 17:59:32

标签: android android-fragments

我知道很多人已经回答了这个问题,但我仍然无法让我的工作。在StartWorkout类中,我从计时器中获取数据,由于该数据以毫秒为单位,因此我使用了一个名为showElapsedtime()的编码方法。将这些毫秒转换为秒,分钟和小时。我想将这些数据传递给另一个名为workouts的类,但由于某种原因它无法正常工作。我没有得到任何数据,因为我得到了一个N​​UllPOINTEREXECPTION

这是我的代码。

StartWorkout类

    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;






      }



 }

这是我的训练课

     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;
    }


}

1 个答案:

答案 0 :(得分:2)

 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();