基本上我是android编程的新手,我正在学习它并且也很有趣。我正在尝试使用单词和卡片来构建一个类似拉米的游戏,并将其模拟为按钮。在这里,我在按钮之间交换值时遇到了问题。
这是我的游戏的模拟,
[A][C][P] [W][D][G]
[K][Z][I]
[empty button]
如果我单击 A 按钮,则 A 按钮的值应替换为空字符串的空按钮。现在 A 在空按钮中,A 按钮应显示空字符串。
我的应用程序在此 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)