【问题标题】:Android league app keeps crashing [closed]Android联赛应用程序不断崩溃[关闭]
【发布时间】:2014-06-03 19:20:40
【问题描述】:

需要一些帮助,创建一个应用程序,发送十个团队名称、获胜和平局的结果进行计算以计算总分,然后应该显示它们 但是在单击继续按钮后,应用程序总是崩溃。 继承人的代码关于什么是错的任何指南 非常感谢

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

}

【问题讨论】:

  • 你得到什么错误?请编辑您的问题以包含您收到的任何错误消息。 “应用程序崩溃”不足以让人们在没有像@selbie 试图做的有根据的猜测的情况下诊断您的问题。如果应用程序确实崩溃了,您是否尝试过自己调试它?包含更多关于您尝试过的内容的详细信息将改善您的问题并帮助您更好地回答未来的问题

标签: java android android-intent counter


【解决方案1】:

Java 数组索引从零开始。

这行代码:

int [] Win = new int[10];

删除包含 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]);   
}

【讨论】:

  • 试过了,一旦按下继续,项目仍然崩溃
  • 我不会为你调试你的程序。我怀疑您在上面的代码路径中有多个错误。我希望看到您努力学习编程、调试、设置断点、分析异常(和日志)以及您正在使用的语言/工具的基础知识。
猜你喜欢
  • 2013-03-26
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2014-03-08
  • 2016-01-27
  • 1970-01-01
相关资源
最近更新 更多