为什么我的应用程序不会移动到下一个类(意图)?

时间:2012-06-29 01:12:18

标签: android android-intent

我一直试着让我的应用程序转移到下一个类“GameOver.class”。有人能指出我做错了什么吗?根据我的日志,OnAnimationEnd方法甚至没有被击中。如果我将它用作主类,GameOver类就可以工作。

public class Rocky extends Activity implements AnimationListener {

TextView m_dog, m_spoon, m_ice_cream, m_car, m_giraffe ;
ImageView rocky_pic, dog, spoon,ice_cream, car, giraffe;
Animation animationFadeOut, animationFadeIn,viewObjFades;
int count = 0;
MediaPlayer mp;
private static final String TAG = "MyActivity";
AlertDialog.Builder builder;
AlertDialog alertDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rocky);

    rocky_pic = (ImageView)findViewById(R.id.rocky_pic);


    Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
    rocky_pic.startAnimation(animationFadeIn);

    m_car=(TextView) findViewById(R.id.m_car);
    m_dog=(TextView) findViewById(R.id.m_dog);
    m_spoon=(TextView) findViewById(R.id.m_spoon);
    m_ice_cream=(TextView) findViewById(R.id.m_ice_cream);
    m_giraffe=(TextView) findViewById(R.id.m_giraffe);

    dog = (ImageView) findViewById(R.id.dog);
    spoon = (ImageView) findViewById(R.id.spoon);
    ice_cream = (ImageView) findViewById(R.id.ice_cream);
    car = (ImageView) findViewById(R.id.car);
    giraffe  = (ImageView) findViewById(R.id.giraffe);

    mp = MediaPlayer.create(Rocky.this, R.raw.ding); 
    dog.setAlpha(0);
    spoon.setAlpha(0);
    ice_cream.setAlpha(0);
    car.setAlpha(0);
    giraffe.setAlpha(0);

}



@Override
public void onStart() {
    super.onStart();

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_dialog,
                                   (ViewGroup) findViewById(R.id.layout_root));

    TextView text = (TextView) layout.findViewById(R.id.text);

    text.setText("Text.text.text");

    builder = new AlertDialog.Builder(this);
    builder.setView(layout);
    alertDialog = builder.create();
    alertDialog.show();

    text.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            alertDialog.dismiss();

        }
    });



    alertDialog.setCanceledOnTouchOutside(true);


    ice_cream.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            ice_cream.setAlpha(200);  
            count ++;
            mp.start();     
            ice_cream.setClickable(false);
            if (count==5){

                fadeout();
            }
        }
    });


    dog.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            dog.setAlpha(200);  
            count ++;
            mp.start();     
            dog.setClickable(false);
            if (count==5){

                fadeout();
            }
        }
    });


    giraffe.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            giraffe.setAlpha(200);  
            count ++;
            mp.start();     
            giraffe.setClickable(false);
            if (count==5){

                fadeout();
            }
        }
    });

    spoon.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            spoon.setAlpha(200);
            count ++;
            mp.start();
            spoon.setClickable(false);
            if (count==5){

                fadeout();
            }
        }
    });     

    car.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            car.setAlpha(200);
            count ++;
            mp.start(); 
            car.setClickable(false);
            if (count==5){

                fadeout();
            }
        }
    });


}



public void fadeout(){


    animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fadeout);
    animationFadeOut.setAnimationListener(this);

    viewObjFades= AnimationUtils.loadAnimation(this, R.anim.trns4view_fadeout);



    car.startAnimation(viewObjFades);
    giraffe.setAnimation(viewObjFades);
    spoon.setAnimation(viewObjFades);
    dog.setAnimation(viewObjFades);
    ice_cream.setAnimation(viewObjFades);


    rocky_pic.startAnimation(animationFadeOut);

    car.setVisibility(View.GONE);
    giraffe.setVisibility(View.GONE);
    spoon.setVisibility(View.GONE);
    dog.setVisibility(View.GONE);
    ice_cream.setVisibility(View.GONE);
    rocky_pic.setVisibility(View.GONE);

}


@Override
public void onAnimationEnd(Animation animation) {
    mp.release();
    Log.v(TAG, "player released");

    Intent end = new Intent(this, GameOver.class);
    Log.v(TAG, "end object created");
    end.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    Log.v(TAG, "no anim");
    end.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    Log.v(TAG, "sing t");
    this.startActivity(end);    
    Log.v(TAG, "begin game over");
}




@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationStart(Animation animation) {

}


@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.rcky_menu, menu);
    return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){


    if(!dog.isClickable()){
        menu.removeItem(R.id.m_dog);
    }
    if(!spoon.isClickable()){
        menu.removeItem(R.id.m_spoon);  
    }

    if(!ice_cream.isClickable()){
        menu.removeItem(R.id.m_ice_cream);
    }

    if(!car.isClickable()){
        menu.removeItem(R.id.m_car);
    }

    if(!giraffe.isClickable()){
        menu.removeItem(R.id.m_giraffe);
    }

    return true;
}
@Override
public void onBackPressed(){

}

}

2 个答案:

答案 0 :(得分:0)

我写了一个方法调用,“next()”并取出“fadeout()”。它现在有效,但我不明白为什么。有人可以解释一下吗?

public void next(){
    mp.release();
    Intent done = new Intent(this, GameOver.class);
    done.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    done.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    this.startActivity(done);
}

答案 1 :(得分:0)

您在启动动画时立即将rocky_pic的可见性设置为Gone。因为它是一个淡出动画,你不应该简单地将动画设置为fill_after,以便在动画结束时它保持不变吗?我认为设置它已经消失,可能会完全停止动画,因此动画被有效取消,这就是为什么onAnimationEnd永远不会被调用bc你通过改变它的可见性有效地使视图无效。