gpt4 book ai didi

java - 添加更多按钮不起作用 - ScoreBook.java

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:21 25 4
gpt4 key购买 nike

我正在尝试为板球制作一个记分簿应用程序。在应用程序的 Team1.java 类中,我尝试填充团队 A,然后填充团队 B。它有一个 EditText 成员,用户可以在其中键入玩家的姓名,然后按“添加更多”按钮将更多玩家添加到 teamPlayers 的 ArrayList 中。问题是当我按下“添加更多”按钮时应用程序崩溃。该代码在下面突出显示。任何帮助,将不胜感激。

Team1.java

package ammar.newscorebook;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Team1 extends Activity {
private String teamName;
private static ArrayList<String> playerList = new ArrayList<String>();

private EditText playerName;
private TextView playerCount;
private Button addButton;
private Button doneButton;

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

setTeamName();
setPlayerNames();
done();
}




private void setTeamName() {
// TODO Gets the name from edittext and sets it to a variable
EditText editText = (EditText) findViewById(R.id.teamName);
editText.addTextChangedListener(new TextWatcher(){

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
teamName = arg0.toString();

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub

}

});
}

private void setPlayerNames() {
// TODO set the player names one by one in the array

playerName = (EditText) findViewById(R.id.playerName);
playerCount = (TextView) findViewById(R.id.player_count_textview);

addButton = (Button) findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String s = playerName.getText().toString();
if(playerName.length() != 0){
playerList.add(s);
playerCount.setText(playerName.length());
playerName.setText(R.string.playername_hint);
}
}
});
}


private void done() {
// TODO Takes to Team2 populate activity
doneButton = (Button) findViewById(R.id.done_button);
doneButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(Team1.this, Team2.class);
String text = "Team " + getTeamName() + " created";
Toast.makeText(Team1.this, text, Toast.LENGTH_SHORT).show();
startActivity(i);

}
});

}



public String getTeamName() {
return teamName;
}


public ArrayList<String> getPlayerList() {
return playerList;
}

}

MainActivity.java

package ammar.newscorebook;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private Button mButton1;

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

mButton1 = (Button)findViewById(R.id.team1_populate_button);
mButton1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Team1.class);
startActivity(i);

}
});

}


}

这是两个 XML 文件。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome" />

<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/team1_populate_button"
android:text="@string/populate_teams" />

<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:id="@+id/match_info_button"
android:text="@string/match_info_label" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:id="@+id/start_scoring_button"
android:text="@string/scorebook" />


</LinearLayout>

activity_populate.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/teamname_label" />

<EditText android:layout_margin="5dp" android:id="@+id/teamName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/teamname_hint">

<requestFocus />
</EditText>

<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="@string/playername_label" />

<EditText android:layout_margin="5dp" android:id="@+id/playerName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="@string/playername_hint" />

<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Player Count: " />

<TextView android:id="@+id/player_count_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" />

</LinearLayout>

<LinearLayout android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center">

<Button style="?android:attr/buttonBarButtonStyle" android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add_button_label" />

<Button style="?android:attr/buttonBarButtonStyle" android:id="@+id/done_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/done_button_label" />


</LinearLayout>

</LinearLayout>

最佳答案

如果在初始化添加按钮之前设置 playerName.length() == 0 怎么样?

这可能会阻止应用崩溃。

关于java - 添加更多按钮不起作用 - ScoreBook.java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com