0

我已经使用下面的代码充气按钮,并想交换使用简单的TranslateAnimation选择的2个按钮的位置。Android:找到充气按钮的ID

代码:

for (int k = 1; k <= quotient; k++) 
    { 
     LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(button_width,row_height); 
     params3.setMargins(button_margin, button_margin, button_margin, button_margin); 
     btn_right = new Button(this); 
     btn_right.setId(idd); 
     final int id_ = btn_right.getId(); 
     btn_right.setText(""+question[idd]); 

     frame_row.addView(btn_right, params3); 

     btn_right.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      {      
       btn_right = ((Button) findViewById(id_)); 
       X1 = getRelativeLeft(btn_right); 
       Y1 = getRelativeTop(btn_right); 
       int b = Integer.parseInt(""+btn_right.getText().toString()); 
       selection ++; 
       if (selection%2 ==1) 
       { 
        selected_id1 = id_; 
        selected_color1 = b;       
        custom_toast("X1=" +X1 + "\nY1=" + Y1);            
       } 
       else 
       { 
        selected_id2 = id_; 
        selected_color2 = b; 
        X2 = getRelativeLeft(btn_right); 
        Y2 = getRelativeTop(btn_right); 

        custom_toast("X2=" +X2 + "\nY2=" + Y2); 

        // the first one 
        btn_right = ((Button) findViewById(selected_id1)); 
        anim1=new TranslateAnimation(0, (X2-X1), 0, (Y2-Y1)); 
        anim1.setFillAfter(true); 
        anim1.setDuration(animationTime); 
        anim1.setAnimationListener(Game.this); 
        btn_right.startAnimation(anim1); 

        // the second one 
        btn_right = ((Button) findViewById(selected_id2)); 
        anim2=new TranslateAnimation(0, (X1-X2), 0, (Y1-Y2)); 
        anim2.setFillAfter(true); 
        anim2.setDuration(animationTime); 
        anim2.setAnimationListener(Game.this); 
        btn_right.startAnimation(anim2);         
       } 

@Override 
public void onAnimationEnd(Animation animation) 
{ 

    if (animation == anim1) 
    {   
     custom_toast("1 clear"); 
     btn_right = ((Button) findViewById(selected_id1)); 
     btn_right.clearAnimation();  // Line 426 

    } 
    if (animation == anim2) 
    {   
     custom_toast("2 clear"); 
     btn_right = ((Button) findViewById(selected_id2)); 
     btn_right.clearAnimation();  
    } 
} 

logcat的:

04-21 21:28:00.728: E/AndroidRuntime(11341): java.lang.NullPointerException 
04-21 21:28:00.728: E/AndroidRuntime(11341): at com.abc.abc.Game.onAnimationEnd(Game.java:426) 

问题:

对于信息,如果选择%2 == 1,这意味着用户已仅按下第一按钮和是尚未准备好交换,并因此记录id及其X,Y坐标。如果选择%2 == 0,则意味着用户已按下2个按钮,并准备调换记录第二的X和Y

此外之后,(X1,Y1)和(X2,Y2)的坐标被正确地报告。

它在(如上所述)进入NPE。我的问题是,由于所有的按钮都是充气的,并且我已经为所有充气按钮设置了标记,为什么它仍然会在第426行用作NPE,或者实际上我应该如何指定用户已经触摸的2个按钮为了实现交换动画?

谢谢!

回答

0

创建按钮的列表,你可以创建按钮并设置其ID,标签和onclicklistenners喜欢这一点,并把它们添加到按钮列表:

buttonList = new ArrayList<Button>(); 

    for (int i=0;i<2;i++){ 
     Button button = new Button(getApplicationContext()); 
     button.setOnClickListener(customListenner); 
     button.setAnimation(anim); 
     button.setId(i); 
     button.setTag(i); 
     myLayout.addView(button); 
     buttonList.add(button); 
    } 

,当你需要再次使用该按钮,就用他们的id或标签从列表中调用。 (buttonList.get(0).getId()

如果您需要不同的监听器,可以通过使用if函数中的唯一标签检查来控制它们并声明另一个动作。

这是当我需要以编程方式在动态视图上创建和使用动画时,总是使用的方法。