-2

i was make button group listener like this but when button clicked the app was force close, i don't know what is wrong with my code so please check this out

this is the full code of my app project

public class menu extends 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();


                }
            });

i was put animation in there so when listener1 button group clicked it will show fadeout animation and when listener1 animation end then listener2 animation show and so on to listener3

and this is from 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)

Im sorry if there is a mistake with my code because that i was ask in here to correct my code, Thanks in Advance

Ricci
  • 217
  • 5
  • 21

1 Answers1

1

You are casting an onClickListenner to a View and try to startAnimation that will cause an error.

Should call it on the view:

view.startAnimation(animFadeout1); 

Other issues with your code:

Since you work with all these buttons it would be easier to make the activity implments OnClickListener:

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

All your buttons use the same id, so it will behave the same for all buttons.

You need to use different ids, and make them use the same listener:

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

Then use a switch case to provide different logic based on the button 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...
       }
   }

You a case for multiple buttons for example:

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

Also your animation are redundant, good coding practice wants you to avoid repetition.

meda
  • 45,103
  • 14
  • 92
  • 122
  • so i can put 4 button into 1 case? and then i can use all of the button to startAnimation in that case? – Ricci Oct 31 '15 at 16:01