片段中的setContentView不起作用

时间:2017-12-11 17:11:27

标签: java android android-fragments

我需要将布局更改为相同的片段。

但是如果我在活动中使用setContentView(使用其他布局),那么IDE会用红色写出来......为什么?

有人可以帮帮我吗?

谢谢!

活动守则:

public class MainActivity extends Activity {

    CheckBox optSingleShot;
    Button btnStart, btnCancel;
    TextView textCounter;
    TextView textCounter2;


    Timer timer;
    MyTimerTask myTimerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        optSingleShot = (CheckBox)findViewById(R.id.singleshot);
        btnStart = (Button)findViewById(R.id.start);
        btnCancel = (Button)findViewById(R.id.cancel);
        textCounter = (TextView)findViewById(R.id.counter);
        //textCounter2 = (TextView)findViewById(R.id.counter2);


        btnStart.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                if(timer != null){
                    timer.cancel();
                }

                //re-schedule timer here
                //otherwise, IllegalStateException of
                //"TimerTask is scheduled already" 
                //will be thrown
                timer = new Timer();
                myTimerTask = new MyTimerTask();

                if(optSingleShot.isChecked()){
                    //singleshot delay 1000 ms
                    timer.schedule(myTimerTask, 1000);
                }else{
                    //delay 1000ms, repeat in 5000ms
                    timer.schedule(myTimerTask, 1000, 1000);
                }
            }});

        btnCancel.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                if (timer!=null){
                    timer.cancel();
                    timer = null;
                }
            }
        });

    }

    class MyTimerTask extends TimerTask {

        @Override
        public void run() {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat simpleDateFormat =
                    //new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
                    new SimpleDateFormat(" HH:mm:ss a");

            final String strDate = simpleDateFormat.format(calendar.getTime());

            //Calendar cal = Calendar.getInstance();
            //cal.setTime(new Date());
            final int hour = calendar.get(Calendar.HOUR_OF_DAY);
            final int minute = calendar.get(Calendar.MINUTE);

            System.out.println("HOUR:"+hour);
            System.out.println("MINUTE:"+minute);
            final String diff = hour + ":" + minute;

            runOnUiThread(new Runnable(){


                @Override
                public void run() {
//135
                /*  if( hour >= 14 && hour <= 17) {
                        if (minute >= 45 && minute <= 59 || minute >= 00 && minute <=59 || minute >= 00 && minute <= 15 ) {
                            textCounter.setText(strDate);

                        } else {
                            timer.cancel();

                        }

                    }else{
                        timer.cancel();

                    }*/

        /*      if( hour >= 00 && hour <=13 ) {
                    System.out.println("U & D ancora non inizia!");
                     if(  hour == 14 && minute >= 00 && minute <= 44  ) {
                            System.out.println("U & D ancora non inizia!");

                    if( hour == 14 && minute >= 45 && minute <= 59 ) {
                        textCounter.setText(strDate);

                        if (hour == 15 && minute >= 00 && minute <= 59 ) {
                            textCounter.setText(strDate);

                            if (hour == 16 && minute >= 00 && minute <= 15) {
                                textCounter.setText(strDate);


                                if (hour == 16 && minute >= 16 && minute <= 59) {
                                    System.out.println("U & D è finito!");
                                    if (hour >=17 && hour <=23) {
                                        System.out.println("U & D è finito!");

                                    } else {
                                        timer.cancel();

                                    }


                                } else {
                                    timer.cancel();

                                }
                            }else {
                                timer.cancel();

                            }
                        }

                            else{
                                timer.cancel();

                            }

                        } else {
                            timer.cancel();

                        }
                        }else {
                            textCounter.setText(strDate);

                        }

                    }else {
                        textCounter.setText(strDate);

                    }*/
//13
                    if( hour >= 00 && hour <=13 ) {
                        //System.out.println("U & D ancora non inizia!");
                /*      Toast.makeText(MainActivity.this, "U & D ancora non inizia!",
                                Toast.LENGTH_LONG).show();*/
                        System.out.println("ENTRATO");

                        setContentView(R.layout.ancoranoninizia);
//14
                    }else if(  hour == 14 && minute >= 00 && minute <= 44  ) {

                        setContentView(R.layout.ancoranoninizia);
//14
                    }else if( hour == 14 && minute >= 45 && minute <= 59 ) {
                        setContentView(R.layout.inmezzo);
                        textCounter2 = (TextView)findViewById(R.id.counter2);

                        textCounter2.setText(strDate);
                        //15
                    }else if (hour == 15 && minute >= 00 && minute <= 59 ) {
                        setContentView(R.layout.inmezzo);
                        textCounter2 = (TextView)findViewById(R.id.counter2);

                        textCounter2.setText(strDate);
                        //16
                    }else if (hour == 16 && minute >= 00 && minute <= 15) {
                        setContentView(R.layout.inmezzo);
                        textCounter2 = (TextView)findViewById(R.id.counter2);

                        textCounter2.setText(strDate);

                    }else if (hour == 16 && minute >= 16 && minute <= 59) {

                        myTimerTask.cancel();
                        setContentView(R.layout.finito);

                        System.out.println("U & D è finito!");
                    }else if (hour >=17 && hour <=23) {

                        myTimerTask.cancel();
                        setContentView(R.layout.finito);

                        Toast.makeText(MainActivity.this, "U & D è finito!",
                                Toast.LENGTH_LONG).show();

                    }
                }});
        }

    }
    @Override
    public void onDestroy() {

        super.onDestroy();

        myTimerTask.cancel();
    }

}

而不是setCOntentView,我必须写什么?

1 个答案:

答案 0 :(得分:0)

setContentView()是一种活动的方法,这就是它在IDE中被写入红色的原因。 要在Fragment中使用布局,必须覆盖片段的onCreateView()

例如:

MultiParamTypeClasses

要将此片段添加到您的活动中,请获取FragmentManager:

public static class ExampleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.example_fragment, container, false);
   }

}

然后,您可以使用add()方法添加片段,指定要添加的片段以及要插入的视图。例如:

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = 
    fragmentManager.beginTransaction();