Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android按钮,同时使用多个按钮交换多个值_Android_Button - Fatal编程技术网

Android按钮,同时使用多个按钮交换多个值

Android按钮,同时使用多个按钮交换多个值,android,button,Android,Button,基本上,我对android编程是新手,我正在学习它,而且很有趣。 我正在尝试建立一个类似拉米的游戏,使用文字和卡片将模拟为按钮。 在这里,我在按钮之间交换值时遇到了一个问题 这是我的游戏模拟 [A][C][P] [W][D][G] [K][Z][I] [empty button] 如果我单击一个按钮,按钮的值应该替换为空按钮,它是空字符串。现在一个按钮在空按钮中,一个按钮应该显示空字符串 我的应用程序抛出错误,因为在这个switch语句中不幸停止工作 public class

基本上,我对android编程是新手,我正在学习它,而且很有趣。 我正在尝试建立一个类似拉米的游戏,使用文字和卡片将模拟为按钮。 在这里,我在按钮之间交换值时遇到了一个问题

这是我的游戏模拟

[A][C][P] [W][D][G]

     [K][Z][I]

  [empty button]
如果我单击一个按钮,按钮的值应该替换为空按钮,它是空字符串。现在一个按钮在空按钮中,一个按钮应该显示空字符串

我的应用程序抛出错误,因为在这个switch语句中不幸停止工作

public class Play extends Activity
{   
int lastCard = RandomAlphabetGenerator.numOfCards; //52
int pos = lastCard;
int OpenCard0 = RandomAlphabetGenerator.OpenCard;  //27
int OpenCard1 = OpenCard0 + 1;

char[] stringarray = null;
char temp;

final Context context = this;

Button button1,button2,button3,button4,button5,button6,button7,button8,button9,buttonM,buttonFREE;

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

RandomAlphabetGenerator.shuffledDecks = RandomAlphabetGenerator.Random();

stringarray = RandomAlphabetGenerator.shuffledDecks.toCharArray();

addListenerOnButton();
}

public void addListenerOnButton() 
{        
button1 = (Button) findViewById(R.id.Button1);
button2 = (Button) findViewById(R.id.Button2);
button3 = (Button) findViewById(R.id.Button3);
button4 = (Button) findViewById(R.id.Button4);
button5 = (Button) findViewById(R.id.Button5);
button6 = (Button) findViewById(R.id.Button6);
button7 = (Button) findViewById(R.id.Button7);
button8 = (Button) findViewById(R.id.Button8);
button9 = (Button) findViewById(R.id.Button9);
buttonM = (Button) findViewById(R.id.showButton);
buttonFREE = (Button) findViewById(R.id.emptyButton);

button1.setText(RandomAlphabetGenerator.shuffledDecks.substring(0,1)); 
button2.setText(RandomAlphabetGenerator.shuffledDecks.substring(1,2)); 
button3.setText(RandomAlphabetGenerator.shuffledDecks.substring(2,3));
button4.setText(RandomAlphabetGenerator.shuffledDecks.substring(3,4)); 
button5.setText(RandomAlphabetGenerator.shuffledDecks.substring(4,5)); 
button6.setText(RandomAlphabetGenerator.shuffledDecks.substring(5,6)); 
button7.setText(RandomAlphabetGenerator.shuffledDecks.substring(6,7)); 
button8.setText(RandomAlphabetGenerator.shuffledDecks.substring(7,8));
button9.setText(RandomAlphabetGenerator.shuffledDecks.substring(8,9));      

buttonM.setText(RandomAlphabetGenerator.shuffledDecks.substring(OpenCard0,OpenCard1));  

buttonFREE.setText("");

View.OnClickListener handler = new OnClickListener() 
{           
public void onClick(View v)
{ 
switch(v.getId())
{
case R.id.Button1:
button1.setText("");
process(0);
break;
case R.id.Button2:
button2.setText("");
process(1);
break;
case R.id.Button3:
button3.setText("");
process(2);
break;
case R.id.Button4:
button4.setText("");
process(3);
break;
case R.id.Button5:
button5.setText("");
process(4);
break;
case R.id.Button6:
button6.setText("");
process(5);
break;
case R.id.Button7:
button7.setText("");
process(6);
break;
case R.id.Button8:
button8.setText("");
process(7);
break;
case R.id.Button9:
button9.setText("");
process(8);
break;
case R.id.showButton:
buttonM.setText("");
process(OpenCard0);
break;
case R.id.emptyButton:
buttonFREE.setText("");
process(lastCard);
break;          
}       
RandomAlphabetGenerator.shuffledDecks = new String(stringarray);
}

private void process(int k) 
{                
int j=k+1;

if(pos==OpenCard0)
{           
buttonM.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j));
}
else if(pos == lastCard)
{                  
buttonFREE.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j));   
}           
else 
{
switch(pos)
{
case 0: button1.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 1: button2.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 2: button3.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 3: button4.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 4: button5.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 5: button6.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 6: button7.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 7: button8.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
case 8: button9.setText(RandomAlphabetGenerator.shuffledDecks.substring(k,j)); break;
}
}

temp = stringarray[k];    
stringarray[k] = stringarray[pos];  //Program stops here  
stringarray[pos] = temp;  
pos = k; 
}
};
findViewById(R.id.Button1).setOnClickListener(handler);
findViewById(R.id.Button2).setOnClickListener(handler);
findViewById(R.id.Button3).setOnClickListener(handler);
findViewById(R.id.Button4).setOnClickListener(handler);
findViewById(R.id.Button5).setOnClickListener(handler);
findViewById(R.id.Button6).setOnClickListener(handler);
findViewById(R.id.Button7).setOnClickListener(handler);
findViewById(R.id.Button8).setOnClickListener(handler);
findViewById(R.id.Button9).setOnClickListener(handler);
findViewById(R.id.emptyButton).setOnClickListener(handler);
findViewById(R.id.showButton).setOnClickListener(handler);
}   
}
以下是logcat显示的内容:

01-03 17:57:59.761: D/AndroidRuntime(705): Shutting down VM
01-03 17:57:59.761: W/dalvikvm(705): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-03 17:57:59.881: E/AndroidRuntime(705): FATAL EXCEPTION: main
01-03 17:57:59.881: E/AndroidRuntime(705): java.lang.ArrayIndexOutOfBoundsException: length=52; index=52
01-03 17:57:59.881: E/AndroidRuntime(705): at apk.games.wordrummy.Play$1.process(Play.java:158)
01-03 17:57:59.881: E/AndroidRuntime(705):  at apk.games.wordrummy.Play$1.onClick(Play.java:83)
01-03 17:57:59.881: E/AndroidRuntime(705):  at android.view.View.performClick(View.java:4084)
01-03 17:57:59.881: E/AndroidRuntime(705):  at android.view.View$PerformClick.run(View.java:16966)
01-03 17:57:59.881: E/AndroidRuntime(705):  at android.os.Handler.handleCallback(Handler.java:615)
01-03 17:57:59.881: E/AndroidRuntime(705):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 17:57:59.881: E/AndroidRuntime(705):  at android.os.Looper.loop(Looper.java:137)
01-03 17:57:59.881: E/AndroidRuntime(705):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-03 17:57:59.881: E/AndroidRuntime(705):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 17:57:59.881: E/AndroidRuntime(705):  at java.lang.reflect.Method.invoke(Method.java:511)
01-03 17:57:59.881: E/AndroidRuntime(705):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-03 17:57:59.881: E/AndroidRuntime(705):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-03 17:57:59.881: E/AndroidRuntime(705):  at dalvik.system.NativeStart.main(Native Method)

java.lang.ArrayIndexOutOfBoundsException:长度=52;索引=52数组导致thisstringarray=RandomAlphabetGenerator.shuffledDecks.ToCharray;这是否会生成一个至少包含53个元素的数组?No.M变量为No_of_Decks=2;卡片数量=卡片数量*26;因此stringarray将从RandomAlphabetGenerator.shuffledDecks.ToCharray存储0到51。