我的ImageViews有问题。 在某个时间我禁用它并使其不可见。在程序的后期,但它们应该重新启用并可见。不幸的是它不起作用。我经常尝试使用调试器找到错误,但不会跳过相应的代码。然而,应用程序中的按钮不可见或启用。即使它应该,即使动画drawable也没有开始。从playanimation()调用时会发生这种情况。
由于这是我的第一个应用程序或程序,代码有点不可读。
也许可以帮助我? 这是代码:
我的意思是代码的一部分:
case 1:
imageViewsleepbutton.setVisibility(View.VISIBLE);
imageViewfeedbutton.setVisibility(View.VISIBLE);
imageViewShowerbutton.setVisibility(View.VISIBLE);
imageViewPlaybutton.setVisibility(View.VISIBLE);
imageViewsleepbutton.setEnabled(true);
imageViewfeedbutton.setEnabled(true);
imageViewShowerbutton.setEnabled(true);
imageViewPlaybutton.setEnabled(true);
Settings.setLevel(2, context);
helper.setCorrectPet();
helper.playAnimation();
textviewStatus.setText(activity.getString(R.string.baby));
break;
在那堂课中:
package at.android.virtualpet;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class TimerC{
//TODO Try-Catch Timer
private Handler handler;
Toast toast;
private ImageView imageViewsleepbutton;
private ImageView imageViewfeedbutton;
private ImageView imageViewShowerbutton;
private ImageView imageViewPlaybutton;
private TextView textviewStatus;
private int intervall;
private Helper helper;
private Context context;
Activity activity;
public TimerC(Context context, Activity activity, int intervall){
this.context = context;
this.activity = activity;
this.intervall = intervall;
}
public void startTimer() {
boolean b, resume;
b = Settings.getFirstStart(context);
resume = Settings.getResumeStatus(context);
handler = new Handler();
helper = new Helper(context, activity);
if (b == true) {
Settings.setFirstStart(false, context);
handler.postDelayed(timedTask, 45000); //45000
}
else {
if (resume == true) {
helper.resumeStats();
Settings.setResumeStatus(false, context);
}
handler.postDelayed(timedTask, intervall);
}
}
public void stopTimer() {
handler.removeCallbacks(timedTask);
}
private void changePet() {
int stufe;
int progress;
imageViewsleepbutton = (ImageView) activity.findViewById(R.id.ImageViewsleepbutton);
imageViewfeedbutton = (ImageView) activity.findViewById(R.id.imageViewfeedbutton);
textviewStatus = (TextView) activity.findViewById(R.id.textViewStatus);
imageViewShowerbutton = (ImageView) activity.findViewById(R.id.imageViewShowerButton);
imageViewPlaybutton = (ImageView) activity.findViewById(R.id.imageViewPlayButton);
stufe = Settings.getLevel(context);
progress = Settings.getLevelProgress(context);
if (progress <= 0 || stufe == 1) {
switch(stufe) {
case 1:
imageViewsleepbutton.setVisibility(View.VISIBLE);
imageViewfeedbutton.setVisibility(View.VISIBLE);
imageViewShowerbutton.setVisibility(View.VISIBLE);
imageViewPlaybutton.setVisibility(View.VISIBLE);
imageViewsleepbutton.setEnabled(true);
imageViewfeedbutton.setEnabled(true);
imageViewShowerbutton.setEnabled(true);
imageViewPlaybutton.setEnabled(true);
Settings.setLevel(2, context);
helper.setCorrectPet();
helper.playAnimation();
textviewStatus.setText(activity.getString(R.string.baby));
break;
case 2:
Settings.setLevel(3, context);
helper.setCorrectPet();
helper.playAnimation();
textviewStatus.setText(activity.getString(R.string.teen));
break;
case 3:
Settings.setLevel(4, context);
helper.setCorrectPet();
helper.playAnimation();
textviewStatus.setText(activity.getString(R.string.adult));
break;
}
Settings.setLevelProgress(1000, context);
}
}
private void changeStats() {
int hunger;
int progress;
int stufe;
int sleep;
int fun;
int dirty;
int level;
int xp;
boolean day;
boolean firststart;
Random randInt = new Random();
int promp = 1; //progress multiplikator
int minushunger = randInt.nextInt(4)+3;
int minussleep = 3;
int sleepnight = 15;
int hungernight = randInt.nextInt(3)+2;
int minusdirty = randInt.nextInt(4)+2;
int minusfun = randInt.nextInt(2)+2;
int dirtynight = minusdirty;
int funnight = minusfun;
hunger = Settings.getHunger(context);
progress = Settings.getLevelProgress(context);
fun = Settings.getFun(context);
dirty = Settings.getDirty(context);
stufe = Settings.getLevel(context);
sleep = Settings.getSleep(context);
level = Settings.getLevel(context);
firststart = Settings.getFirstStart(context);
xp = Settings.getXP(context);
day = Settings.getDay(context);
if ((firststart == false) && (level != 1)) {
if (day == true) {
hunger = hunger - minushunger;
sleep = sleep - minussleep;
dirty = dirty - minusdirty;
fun = fun - minusfun;
} else {
hunger = hunger - hungernight;
sleep = sleep + sleepnight;
dirty = dirty - dirtynight;
fun = fun - funnight;
}
if(sleep > 100) {
sleep = 100;
helper.wakeup();
}
if(hunger < -5 ) {hunger = -5;}
if(sleep < -5) {sleep = -5;}
if(hunger > 110) {hunger = 110;}
if(dirty < -15 ) {dirty = -15;}
if(fun < -15 ) {fun = -15;}
if(dirty > 110) {dirty = 110;}
if(fun > 110) {fun = 110;}
Settings.setSleep(sleep, context);
Settings.setHunger(hunger, context);
Settings.setDirty(dirty, context);
Settings.setFun(fun, context);
//promp = 1
if ((sleep < 0) && (hunger < 0) && (dirty < 20) && (fun < 20)) {promp = 0;};
if ((sleep > 90) && (hunger > 90) && (dirty > 75) && (fun > 75)) {promp = 2;};
switch(stufe) {
case 1:
//Stufe 1 ist Ei. Das Ei schlüpft automatisch nach x Sek
break;
case 2:
progress = progress - (5 * promp);
break;
case 3:
progress = progress - (4 * promp);
break;
case 4:
progress = progress - (3 * promp);
break;
}
xp = xp + 4*promp;
Settings.setXP(xp, context);
helper.setCorrectPet();
helper.checkLevel();
Settings.setLevelProgress(progress, context);
helper.checkStatus();
}
}
private Runnable timedTask = new Runnable(){
@Override
public void run() {
changePet();
changeStats(); //Werte ändern und Meldungen ausgeben
helper.updateprogress();
helper.updateDebugFields();
handler.postDelayed(timedTask, intervall);
}};
}
和另一个类中的playanimation()。此类的上下文是MainActivity的活动。
public void playAnimation() {
imageViewPet = (ImageView)activity.findViewById(R.id.imageViewPet);
if (imageViewPet.getDrawable() instanceof AnimationDrawable) {
animatedaotori = (AnimationDrawable)imageViewPet.getDrawable();
Handler handler = new Handler(activity.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
animatedaotori.start();
}
}, 1500);
}
}