单击按钮时应用程序强制关闭

时间:2015-10-31 15:33:34

标签: java android button android-animation

我是这样制作按钮组听众,但是当点击按钮时,应用程序强制关闭,我不知道我的代码有什么问题所以请检查出来

这是我的应用项目的完整代码

公共类菜单扩展了Activity {

     static String KEY_ANIM = "TARGET_ANIM";
     static String Target_Move = "Move";
     static String Target_Alpha = "Alpha";
     String target_op = Target_Move;

    Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12;   
    Animation animFadein, animFadeout, animFadeout1, animFadeout2, animFadeout3;
    OnClickListener listener1, listener2, listener3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.menu);

        animFadeout = AnimationUtils.loadAnimation(this, R.animator.fadeout);
        animFadeout1 = AnimationUtils.loadAnimation(this, R.animator.fadeout);
        animFadeout2 = AnimationUtils.loadAnimation(this, R.animator.fadeout);
        animFadeout3 = AnimationUtils.loadAnimation(this, R.animator.fadeout);
        animFadein = AnimationUtils.loadAnimation(this, R.animator.fadein);

        button1= (Button)findViewById(R.id.aa1);             
        button2= (Button)findViewById(R.id.aa2);    
        button3= (Button)findViewById(R.id.aa3);             
        button4= (Button)findViewById(R.id.aa4);                    
        button5= (Button)findViewById(R.id.aa5);        
        button6= (Button)findViewById(R.id.aa6);             
        button7= (Button)findViewById(R.id.aa7);             
        button8=(Button)findViewById(R.id.aa8);                 
        button9= (Button)findViewById(R.id.aa9);    
        button10= (Button)findViewById(R.id.aa10);           
        button11= (Button)findViewById(R.id.aa11);           
        button12= (Button)findViewById(R.id.aa12);



          listener1 = new OnClickListener() {
                @Override
                public void onClick(View one) {
                   target_op = Target_Alpha; 

                   if(one==button1|| one==button2|| one==button3|| one==button4){

                       ((View) listener1).startAnimation(animFadeout1);                

                     }                                   
                }
           };

    button1.setOnClickListener(listener1);
    button2.setOnClickListener(listener1);
    button3.setOnClickListener(listener1);
    button4.setOnClickListener(listener1);

                  listener2 = new OnClickListener() {
                        @Override
                        public void onClick(View two) {
                           target_op = Target_Alpha;

                           ((View) listener2).startAnimation(animFadeout);

                           if(two==button5|| two==button6|| two==button7|| two==button8){

                                Intent intent = new Intent( Amenu.this, bachievements.class);
                                intent.putExtra(KEY_ANIM, target_op);
                                startActivity(intent);
                             }
                         }                                                      
               };

    button5.setOnClickListener(listener2);
    button6.setOnClickListener(listener2);
    button7.setOnClickListener(listener2);
    button8.setOnClickListener(listener2);

                  listener3 = new OnClickListener() {
                        @Override
                        public void onClick(View three) {
                           target_op = Target_Alpha;

                           ((View) listener3).startAnimation(animFadeout);

                           if(three==button9|| three==button10|| three==button11|| three==button12){

                             }
                         }

               };



    button9.setOnClickListener(listener2);
    button10.setOnClickListener(listener2);
    button11.setOnClickListener(listener2);
    button12.setOnClickListener(listener2);

               animFadeout1.setAnimationListener(new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                           ((View) listener2).startAnimation(animFadeout2);
                    }
                });


           animFadeout2.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                       ((View) listener2).startAnimation(animFadeout3);
                }
            });


           animFadeout3.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub

                          Intent intent = new Intent( menu.this, play.class);
                          intent.putExtra(KEY_ANIM, target_op);
                          startActivity(intent);
                          menu.this.finish();


                }
            });

我把动画放在那里所以当listener1按钮组点击它会显示fadeout动画,当listener1动画结束然后listener2动画节目等等到listener3

这是来自logcat

10-31 22:53:57.375: E/AndroidRuntime(642): FATAL EXCEPTION: main
10-31 22:53:57.375: E/AndroidRuntime(642): java.lang.ClassCastException: com.mira.canyoutapit.Amenu$2 cannot be cast to android.view.View
10-31 22:53:57.375: E/AndroidRuntime(642):  at com.mira.canyoutapit.Amenu$2.onClick(Amenu.java:93)
10-31 22:53:57.375: E/AndroidRuntime(642):  at android.view.View.performClick(View.java:4084)
10-31 22:53:57.375: E/AndroidRuntime(642):  at android.view.View$PerformClick.run(View.java:16966)
10-31 22:53:57.375: E/AndroidRuntime(642):  at android.os.Handler.handleCallback(Handler.java:615)
10-31 22:53:57.375: E/AndroidRuntime(642):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 22:53:57.375: E/AndroidRuntime(642):  at android.os.Looper.loop(Looper.java:137)
10-31 22:53:57.375: E/AndroidRuntime(642):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 22:53:57.375: E/AndroidRuntime(642):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 22:53:57.375: E/AndroidRuntime(642):  at java.lang.reflect.Method.invoke(Method.java:511)
10-31 22:53:57.375: E/AndroidRuntime(642):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 22:53:57.375: E/AndroidRuntime(642):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 22:53:57.375: E/AndroidRuntime(642):  at dalvik.system.NativeStart.main(Native Method)

我很抱歉,如果我的代码出错,因为我在这里要求更正我的代码,在此先感谢

1 个答案:

答案 0 :(得分:1)

您正在向onClickListenner投射View并尝试startAnimation,这会导致错误。

应该在视图上调用它:

view.startAnimation(animFadeout1); 

您的代码存在其他问题:

由于您使用所有这些按钮,因此更容易制作活动展示位置OnClickListener

public class menu extends Activity implements View.OnClickListener{...}

所有按钮都使用相同的ID,因此所有按钮的行为都相同。

您需要使用不同的ID,并使它们使用相同的侦听器:

button1= (Button)findViewById(R.id.button_one);   
button1.setOnClickListener(this);          
button2= (Button)findViewById(R.id.button_two); 
button2.setOnClickListener(this);

然后使用开关盒根据按钮ID提供不同的逻辑。

  @Override
   public void onClick(View view) {
       //To identify the correct widget, use the getId() method on the view argument
       int id = view.getId();
       switch (id) {
           case R.id.button_one:
               //viewOne clicked
               break;
           case R.id.button_two:
               //And so on...
       }
   }

您可以使用多个按钮,例如:

case R.id.button_one, R.id.button_two , R.id.button_three, R.id.button_four:

此外,您的动画也是多余的,良好的编码习惯要求您避免重复。