需要一些帮助,创建一个应用程序,发送十个团队名称的结果,赢得和绘制的游戏进行计算以计算总分然后应该显示它们 但点击继续按钮后,应用程序始终崩溃。 下面的代码是什么指南什么都出错了 非常感谢
package com.example.leaguetest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.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 android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.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
}
答案 0 :(得分:1)
Java数组索引基于零。
这行代码:
int [] Win = new int[10];
Delcares包含10个项目。这些项目的索引范围为0..9。
这行代码:
Win[10] = Integer.parseInt(SWin[10]);
尝试为数组指定超出范围的值。我还怀疑在此之前访问SWin [10]崩溃了。
我想你想说的是:
for (int index = 0; index < (SWin.length) && (index < 10); index++)
{
Win[index] = Integer.ParseInt(SWin[index]);
}