Java 添加视图时应用程序崩溃
我正在构建一个简单的“black jack”应用程序,我有一个令人讨厌的小错误。 我有两个线性布局,我给它们添加了卡片(每个布局有3张卡片) 有时应用程序崩溃,日志显示“index=1 count=0”。它发生在我尝试将卡片添加到第二个布局时 有时它确实有效,游戏也有效。。 这是有bug的类:Java 添加视图时应用程序崩溃,java,android,android-layout,Java,Android,Android Layout,我正在构建一个简单的“black jack”应用程序,我有一个令人讨厌的小错误。 我有两个线性布局,我给它们添加了卡片(每个布局有3张卡片) 有时应用程序崩溃,日志显示“index=1 count=0”。它发生在我尝试将卡片添加到第二个布局时 有时它确实有效,游戏也有效。。 这是有bug的类: private void computerTurn() { if(x<=4 && computer.getCardsSum()<=16) flag=
private void computerTurn() {
if(x<=4 && computer.getCardsSum()<=16)
flag= true;
if(x<=4 && computer.getCardsSum()>16 )
flag= false;
if(x>=5 && computer.getCardsSum()<=18)
flag= true;
if(x>=5 && computer.getCardsSum()>18)
flag=false;
if(computer.getCardsSum()>=21)
flag=false;
if (flag == false && lastTurn == false) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.topcardview, null);
AssetManager assat = getAssets();
InputStream steam;
ImageView img = (ImageView) view.findViewById(R.id.imageView1);
cards tempCard = dealer.getDelersCards().get(0);
computer.addCardToPlayer(dealer.getDelersCards().remove(0));
computerCardSum += tempCard.getValue();
try {
if (cameFromLastTurn == false)
steam = assat.open("cards/cardsBack.png");
else
steam = assat.open(tempCard.getImgPath());
Drawable d = Drawable.createFromStream(steam, null);
img.setImageDrawable(d);
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
myLayoutParams.leftMargin = -15;
myLayoutParams.rightMargin = -20;
myLayoutParams.width =width/8 ;
myLayoutParams.height = height/6;
img.setLayoutParams(myLayoutParams);
if (computerCardIndex < 3) {
playerTwoLinearLayout.addView(view, computerCardIndex);
lastTurn = true;
computerCardValueSum.setText("sum= ?");
computerCardIndex++;
playersSecondRowIndex=0;
} else {
playerTwoSecondRowLinearLayout.addView(view, computerSecondRowIndex);
lastTurn = true;
computerCardValueSum.setText("sum= ?");
computerSecondRowIndex++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (flag == true) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.topcardview, null);
AssetManager assat = getAssets();
InputStream steam;
ImageView img = (ImageView) view.findViewById(R.id.imageView1);
cards tempCard = dealer.getDelersCards().get(0);
computer.addCardToPlayer(dealer.getDelersCards().remove(0));
computerCardSum += tempCard.getValue();
try {
if (cameFromLastTurn == false)
steam = assat.open("cards/cardsBack.png");
else
steam = assat.open(tempCard.getImgPath());
Drawable d = Drawable.createFromStream(steam, null);
img.setImageDrawable(d);
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
myLayoutParams.leftMargin = -15;
myLayoutParams.rightMargin = -20;
myLayoutParams.width =width/8 ;
myLayoutParams.height = height/6;
img.setLayoutParams(myLayoutParams);
if (computerCardIndex < 3) {
playerTwoLinearLayout.addView(view, computerCardIndex);
computerCardValueSum.setText("sum= ?");
computerCardIndex++;
playersSecondRowIndex=0;
} else {
playerTwoSecondRowLinearLayout.addView(view, playersSecondRowIndex);
computerCardValueSum.setText("sum= ?");
playersSecondRowIndex++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
hitMe.setOnClickListener(hitMeListener);
}
谢谢:)这里提到了罪犯:
E/AndroidRuntime(8981): java.lang.IndexOutOfBoundsException: index=1 count=0
E/AndroidRuntime(8981): at android.view.ViewGroup.addInArray(ViewGroup.java:3452)
E/AndroidRuntime(8981): at android.view.ViewGroup.addViewInner(ViewGroup.java:3387)
E/AndroidRuntime(8981): at android.view.ViewGroup.addView(ViewGroup.java:3236)
E/AndroidRuntime(8981): at android.view.ViewGroup.addView(ViewGroup.java:3181)
E/AndroidRuntime(8981): at com.example.homework_cardgame.Main_CardGame_Activity.computerTurn(Main_CardGame_Activity.java:346)
因此,基本上您得到的是空数组(
count=0
),但您试图获取位置1处的元素,因此在Main\u CardGame\u Activity.java
line 346Main\u CardGame\u Activity.java:346这是哪一行?您在错误日志Main\u CardGame\u Activity.computerTurn()中发布了答案正在引发边界异常的索引。您有0个条目,正在尝试访问索引1。你在发帖前看过吗?我不明白。。。它说问题是在数组中还是在添加视图中?这是第346行:playerWOSecondRowLinearLayout.addView(视图,playerSecondRowIndex);我发现了问题。。。菜鸟的错误。。在:playerWOSecondRowLinearLayout.addView(视图,playerSecondRowIndex);我使用playerSecondRowIndex,我在其他类中也使用它。。。我改了,到目前为止没有问题。。。thx的帮助:)
E/AndroidRuntime(8981): java.lang.IndexOutOfBoundsException: index=1 count=0
E/AndroidRuntime(8981): at android.view.ViewGroup.addInArray(ViewGroup.java:3452)
E/AndroidRuntime(8981): at android.view.ViewGroup.addViewInner(ViewGroup.java:3387)
E/AndroidRuntime(8981): at android.view.ViewGroup.addView(ViewGroup.java:3236)
E/AndroidRuntime(8981): at android.view.ViewGroup.addView(ViewGroup.java:3181)
E/AndroidRuntime(8981): at com.example.homework_cardgame.Main_CardGame_Activity.computerTurn(Main_CardGame_Activity.java:346)