应用程序崩溃视图添加

时间:2014-01-07 15:54:10

标签: java android android-layout

我正在构建一个简单的“黑杰克”应用程序,我有这个小烦人的bug。 我有两个线性布局女巫我添加卡(每个布局有3张卡)。

有时应用程序崩溃,日志显示“index = 1 count = 0”。当我尝试将卡添加到第二个布局时,就会发生这种情况。

有时它确实有效并且游戏有效...... 这是带有错误的类:

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);
}

这是日志猫:

01-07 17:45:33.690: E/AndroidRuntime(8981): FATAL EXCEPTION: main
01-07 17:45:33.690: E/AndroidRuntime(8981): java.lang.IndexOutOfBoundsException: index=1 count=0
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.view.ViewGroup.addInArray(ViewGroup.java:3452)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3387)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.view.ViewGroup.addView(ViewGroup.java:3236)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.view.ViewGroup.addView(ViewGroup.java:3181)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at com.example.homework_cardgame.Main_CardGame_Activity.computerTurn(Main_CardGame_Activity.java:346)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at com.example.homework_cardgame.Main_CardGame_Activity.access$0(Main_CardGame_Activity.java:257)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at com.example.homework_cardgame.Main_CardGame_Activity$1$1.run(Main_CardGame_Activity.java:244)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.os.Handler.handleCallback(Handler.java:725)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.os.Looper.loop(Looper.java:158)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at android.app.ActivityThread.main(ActivityThread.java:5751)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at java.lang.reflect.Method.invoke(Method.java:511)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
01-07 17:45:33.690: E/AndroidRuntime(8981):     at dalvik.system.NativeStart.main(Native Method)
01-07 17:45:35.430: D/Process(8981): killProcess, pid=8981
01-07 17:45:35.430: D/Process(8981): dalvik.system.VMStack.getThreadStackTrace(Native Method)
01-07 17:45:35.430: D/Process(8981): java.lang.Thread.getStackTrace(Thread.java:599)
01-07 17:45:35.430: D/Process(8981): android.os.Process.killProcess(Process.java:1004)
01-07 17:45:35.430: D/Process(8981): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:123)
01-07 17:45:35.430: D/Process(8981): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
01-07 17:45:35.430: D/Process(8981): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
01-07 17:45:35.430: D/Process(8981): dalvik.system.NativeStart.main(Native Method)

谢谢:)

1 个答案:

答案 0 :(得分:3)

你在这里提到了罪魁祸首:

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_CardGame_Activity.java第346行中的代码引起异常