私はレイアウトを同じフラグメントに変更する必要があります。フラグメントのsetContentViewが機能しません
しかし、私がsetContentView
(別のレイアウトで)を使っている場合、それはIDEによって赤で書かれています...なぜですか?
誰かが私を助けることができますか?
感謝!活動のため
CODE:
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、私が書かなければならないでしょうか?
既に試したことを示すと、さらに役立つかもしれません。 – Steven
フラグメントでsetContentViewを使用しないため、 'onCreateView'をオーバーライドしてからビューを展開します。 – tyczj
@tyczj更新コード – Sisso