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

时间:2015-06-07 23:52:36

标签: java android xml debugging crash

我正在尝试为Cricket制作一个记分牌应用程序。在应用程序的Team1.java类中,我尝试填充团队A然后填充B.它有一个EditText成员,用户可以在其中键入播放器的名称,然后按Add More按钮将更多播放器添加到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>

1 个答案:

答案 0 :(得分:0)

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

这可能会阻止应用程序崩溃。