关于在TextView中使用方法的问题。
使用Java,我在第1周到第7周写了一个夹具列表(在一个单独的Fixture类中),这将得出足球分数。分数将保存在一个数组中,我将在其中创建Fixtures类的实例并显示结果。这在标准的Eclipse控制台输出上运行良好,但在Android中将其付诸实践存在问题。
我认为问题出在转换中,因为通常我会使用String.valueOf转换为TextView输出的字符串。但是这对我当前的方法不起作用。这是迄今为止的代码:
package com.example.papersoccer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
Fixtures fixtures = new Fixtures();
String[] teamResults = new String[8];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickGoToLeagueSummary(View view)
{
Intent intent = new Intent(MainActivity.this, ResultsActivity.class);
startActivity(intent);
}
public void onClickDisplayResults(View view)
{
TextView txtDisplayResults;
txtDisplayResults = (TextView)findViewById(R.id.textDisplayResults);
fixtures.teamFixturesW1(teamResults);
txtDisplayResults.setText(String.valueOf(fixtures.teamFixturesW1(teamResults)));
}
@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;
}
}
有一条红色错误行,上面写着“String.valueOf”。我怀疑它无法将方法转换为字符串。是否有替代方法可以将方法中保存的数据显示给TextView?
编辑: 这是方法中包含的所有4个游戏的代码,如果它可以帮助任何人。
package com.example.papersoccer;
import java.util.Random;
public class Fixtures
{
Random random = new Random();
int resultHome = 0; //stores home team result
int resultAway = 0; //stores away team result
String[] myTeams = new String[8]; //stores an array of string teams
int[] goalsScored = new int[8]; //stores an int array of goals each team scored
int[] pointsAttained = new int[8]; //stores an int array of points attained by each team
public void teamFixturesW1(String[] teamResults)
{
myTeams[0] = "Manchester Utd"; //A
myTeams[1] = "AC Milan"; //B
myTeams[2] = "Celtic Fc"; //C
myTeams[3] = "Shakhtar Donestk"; //D
myTeams[4] = "Juventus"; //E
myTeams[5] = "Chelsea"; //F
myTeams[6] = "Barcelona"; //G
myTeams[7] = "Real Madrid"; //H
//1st Game
for (int i = 0; i < 1; i++)
{
//assign variables random generated numbers
resultHome = random.nextInt(0 + 4);
resultAway = random.nextInt(0 + 4);
System.out.println(myTeams[2] + " " + resultHome + " " + "- " + resultAway + " " + myTeams[3]);
//set conditions for win, loss and draw
if (resultHome > resultAway)
{
System.out.println(myTeams[2] + " have clinched 3 points in an epic game!!\n");
pointsAttained[2] += 3; //add 3 points for a win
goalsScored[2] += resultHome; //increment goals by home team
goalsScored[3] += resultAway; //increment goals scored by away team
}
else if(resultAway > resultHome) //set condition for away team win
{
System.out.println(myTeams[3] + " have clinched 3 points in an epic game!!\n");
pointsAttained[3] += 3;
goalsScored[2] += resultHome;
goalsScored[3] += resultAway;
}
else if(resultHome == resultAway) //set condition for draw
{
System.out.println("Both teams draw and go away with 1 point each!!\n");
pointsAttained[2] +=1;
pointsAttained[3] +=1;
goalsScored[2] += resultHome;
goalsScored[3] += resultAway;
}
else
{
System.out.println("Game abandoned due to severe weather conditions."); //game postponed
}
}
//2nd Game
for (int i = 0; i < 1; i++)
{
//assign variables random generated numbers
resultHome = random.nextInt(0 + 4);
resultAway = random.nextInt(0 + 4);
System.out.println(myTeams[7] + " " + resultHome + " " + "- " + resultAway + " " + myTeams[0]);
//set conditions for win, loss and draw
if (resultHome > resultAway)
{
System.out.println(myTeams[7] + " have clinched 3 points in an epic game!!\n");
pointsAttained[7] += 3; //add 3 points for a win
goalsScored[7] += resultHome; //increment scored by home team
goalsScored[0] += resultAway; //increment goals scored by away team
}
else if(resultAway > resultHome) //set condition for away team win
{
System.out.println(myTeams[0] + " have clinched 3 points in an epic game!!\n");
pointsAttained[0] += 3;
goalsScored[7] += resultHome;
goalsScored[0] += resultAway;
}
else if(resultHome == resultAway) //set condition for draw
{
System.out.println("Both teams draw and go away with 1 point each!!\n");
pointsAttained[7] +=1;
pointsAttained[0] +=1;
goalsScored[7] += resultHome;
goalsScored[0] += resultAway;
}
else
{
System.out.println("Game abandoned due to severe weather conditions."); //game postponed
}
}
//3rd game
for (int i = 0; i < 1; i++)
{
//assign variables random generated numbers
resultHome = random.nextInt(0 + 4);
resultAway = random.nextInt(0 + 4);
System.out.println(myTeams[6] + " " + resultHome + " " + "- " + resultAway + " " + myTeams[4]);
//set conditions for win, loss and draw
if (resultHome > resultAway)
{
System.out.println(myTeams[6] + " have clinched 3 points in an epic game!!\n");
pointsAttained[6] += 3; //add 3 points for a win
goalsScored[6] += resultHome; //increment scored by home team
goalsScored[4] += resultAway; //increment goals scored by away team
}
else if(resultAway > resultHome) //set condition for away team win
{
System.out.println(myTeams[4] + " have clinched 3 points in an epic game!!\n");
pointsAttained[4] += 3;
goalsScored[6] += resultHome;
goalsScored[4] += resultAway;
}
else if(resultHome == resultAway) //set condition for draw
{
System.out.println("Both teams draw and go away with 1 point each!!\n");
pointsAttained[6] +=1;
pointsAttained[4] +=1;
goalsScored[6] += resultHome;
goalsScored[4] += resultAway;
}
else
{
System.out.println("Game abandoned due to severe weather conditions."); //game postponed
}
}
//4th game
for (int i = 0; i < 1; i++)
{
//assign variables random generated numbers
resultHome = random.nextInt(0 + 4);
resultAway = random.nextInt(0 + 4);
System.out.println(myTeams[5] + " " + resultHome + " " + "- " + resultAway + " " + myTeams[1]);
//set conditions for win, loss and draw
if (resultHome > resultAway)
{
System.out.println(myTeams[5] + " have clinched 3 points in an epic game!!\n");
pointsAttained[5] += 3; //add 3 points for a win
goalsScored[5] += resultHome; //increment scored by home team
goalsScored[1] += resultAway; //increment goals scored by away team
}
else if(resultAway > resultHome) //set condition for away team win
{
System.out.println(myTeams[1] + " have clinched 3 points in an epic game!!\n");
pointsAttained[1] += 3;
goalsScored[5] += resultHome;
goalsScored[1] += resultAway;
}
else if(resultHome == resultAway) //set condition for draw
{
System.out.println(" Both teams draw and go away with 1 point each!!\n");
pointsAttained[5] +=1;
pointsAttained[1] +=1;
goalsScored[5] += resultHome;
goalsScored[1] += resultAway;
}
else
{
System.out.println("Game abandoned due to severe weather conditions."); //game postponed
}
}
}//end of method
我知道它的编写非常糟糕,但一旦我更好地理解OOP,我就会修复。
答案 0 :(得分:0)
您的teamFixturesW1()
方法中会出现一些错误:
void
- 这是OP中问题的原因,您无法调用String.valueOf(null)
String[] teamResults
从未使用过 - 这是打算用的吗?我认为要解决您的问题,您应该执行以下操作:
public String teamFixturesW1(String[] teamResults){
String results = "";
...
return results;
}
然后在results
字符串中填入您要在TextView