有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android league应用程序不断崩溃

需要一点帮助,创建一个应用程序,发送十个球队名称、赢得的比赛和平局的结果,并进行计算得出总积分,然后显示它们 但单击“继续”按钮后,应用程序总是崩溃。 这是代码,有什么错误的指南吗 非常感谢

package com.example.leaguetest;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.widget.TextView;


public class Display extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    //get the the BUNDLE inside the intent
    Bundle m = this.getIntent().getExtras();

    String [] TeamName = m.getStringArray("TeamName");
    String[] SWin = m.getStringArray("Win");
    String[] SDraw =  m.getStringArray("Draw");
    int [] Win = new int[10];
    int [] Draw = new int [10];


    // Convert to from string to integer Win
    Win[1] = Integer.parseInt(SWin[1]);
    Win[2] = Integer.parseInt(SWin[2]);
    Win[3] = Integer.parseInt(SWin[3]);
    Win[4] = Integer.parseInt(SWin[4]);
    Win[5] = Integer.parseInt(SWin[5]);
    Win[6] = Integer.parseInt(SWin[6]);
    Win[7] = Integer.parseInt(SWin[7]);
    Win[9] = Integer.parseInt(SWin[8]);
    Win[9] = Integer.parseInt(SWin[9]);
    Win[10] = Integer.parseInt(SWin[10]);
    // Convert to from string to integer Draw
    Draw[1] = Integer.parseInt(SDraw[1]);
    Draw[2] = Integer.parseInt(SDraw[2]);
    Draw[3] = Integer.parseInt(SDraw[3]);
    Draw[4] = Integer.parseInt(SDraw[4]);
    Draw[5] = Integer.parseInt(SDraw[5]);
    Draw[6] = Integer.parseInt(SDraw[6]);
    Draw[7] = Integer.parseInt(SDraw[7]);
    Draw[8] = Integer.parseInt(SDraw[8]);
    Draw[9] = Integer.parseInt(SDraw[9]);
    Draw[10] = Integer.parseInt(SDraw[10]);


    //Calculation

    int Result1 = (Win[1]* 3)+Draw[1];



    TextView Result1T=(TextView)findViewById(R.id.TextView1);
    Result1T.setText(TeamName[1] );         

    TextView Result2T=(TextView)findViewById(R.id.TextView2);
    Result2T.setText( Result1);










}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display, menu);
    return true;
}




}

package com.example.leaguetest;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.widget.EditText;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


 //global Variables
String [] TeamName = new String[9];
String [] Win = new String[9];
String [] Draw = new String [9];
int counter = 0;

public void Save(View view) {

    EditText Team =(EditText)findViewById(R.id.editTeamName);
    EditText WinG=(EditText)findViewById(R.id.editWin);
    EditText DrawG=(EditText)findViewById(R.id.editDraw);
    EditText LossG=(EditText)findViewById(R.id.editLoss);


    //If teams not greater than 10 then
    if(counter < 9){

        //putting inputs into array
        TeamName[counter] = Team.getText().toString();
        Win[counter]= WinG.getText().toString();
        Draw[counter]= DrawG.getText().toString();



        counter ++;
    }

    //reset text boxes

        Team.setText("");
        WinG.setText("");
        DrawG.setText("");
        LossG.setText("");
    }//end if



public void Continue(View view) {

    //bundle
    Bundle myBundle =  new Bundle();
    Intent myIntent = new Intent(this, Display.class);

    //put arrays into the bundle
    myBundle.putStringArray("Name", TeamName);
    myBundle.putStringArray("Win", Win);
    myBundle.putStringArray("Draw", Draw);

    //put the bundle into your intent
    myIntent.putExtras(myBundle);

    //start the activity as defined in the intent
    startActivity(myIntent);

}//end saveNameGrade

}


共 (1) 个答案

  1. # 1 楼答案

    Java数组索引是基于零的

    这行代码:

    int [] Win = new int[10];
    

    Delcares包含10个项目的数组。这些项目的索引范围为0。。九,

    这行代码:

    Win[10] = Integer.parseInt(SWin[10]);
    

    尝试为数组指定超出范围的值。我还怀疑,在此之前,访问SWin[10]会崩溃

    我想你想说的是:

    for (int index = 0; index < (SWin.length) && (index < 10); index++)
    {
       Win[index] = Integer.ParseInt(SWin[index]);   
    }