2015-06-07 241 views
1

我正在嘗試爲Cricket製作評分簿應用程序。在應用程序的Team1.java類中,我嘗試填充A和B然後填充。它有一個EditText成員,用戶可以鍵入玩家的名字,然後按下Add More按鈕將更多玩家添加到teamPlayers的ArrayList中。問題是,當我按下添加按鈕時,應用程序會崩潰。代碼在下面突出顯示。任何幫助,將不勝感激。添加更多按鈕不工作 - ScoreBook.java

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 { 
 
\t private String teamName; 
 
\t private static ArrayList<String> playerList = new ArrayList<String>(); 
 
\t 
 
\t private EditText playerName; 
 
\t private TextView playerCount; 
 
\t private Button addButton; 
 
\t private Button doneButton; 
 
\t 
 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState){ 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.activity_populate); 
 
\t \t 
 
\t \t setTeamName(); 
 
\t \t setPlayerNames(); 
 
\t \t done(); 
 
\t } 
 

 
\t 
 

 

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

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

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

 
\t \t \t @Override 
 
\t \t \t public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
 
\t \t \t \t \t int arg3) { 
 
\t \t \t \t // TODO Auto-generated method stub 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t \t 
 
\t \t }); 
 
\t } 
 
\t 
 
\t private void setPlayerNames() { 
 
\t \t // TODO set the player names one by one in the array 
 
\t \t 
 
\t \t \t playerName = (EditText) findViewById(R.id.playerName); 
 
\t \t \t playerCount = (TextView) findViewById(R.id.player_count_textview); 
 
\t \t \t 
 
\t \t \t addButton = (Button) findViewById(R.id.add_button); 
 
\t \t \t addButton.setOnClickListener(new View.OnClickListener() { 
 
\t \t \t \t 
 
\t \t \t \t @Override 
 
\t \t \t \t public void onClick(View v) { 
 
\t \t \t \t \t String s = playerName.getText().toString(); 
 
\t \t \t \t \t if(playerName.length() != 0){ 
 
\t \t \t \t \t \t playerList.add(s); 
 
\t \t \t \t \t \t playerCount.setText(playerName.length()); 
 
\t \t \t \t \t \t playerName.setText(R.string.playername_hint); \t 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t }); 
 
\t \t } 
 
\t \t \t 
 

 
\t private void done() { 
 
\t \t // TODO Takes to Team2 populate activity 
 
\t \t doneButton = (Button) findViewById(R.id.done_button); 
 
\t \t doneButton.setOnClickListener(new View.OnClickListener() { 
 
\t \t \t 
 
\t \t \t @Override 
 
\t \t \t public void onClick(View v) { 
 
\t \t \t \t Intent i = new Intent(Team1.this, Team2.class); 
 
\t \t \t \t String text = "Team " + getTeamName() + " created"; 
 
\t \t \t \t Toast.makeText(Team1.this, text, Toast.LENGTH_SHORT).show(); 
 
\t \t \t \t startActivity(i); 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t }); 
 
\t \t 
 
\t } 
 

 

 

 
\t public String getTeamName() { 
 
\t \t return teamName; 
 
\t } 
 
\t 
 

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

 
}

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 { 
 
\t 
 
\t private Button mButton1; 
 

 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.activity_main); 
 
\t \t 
 
\t \t mButton1 = (Button)findViewById(R.id.team1_populate_button); 
 
\t \t mButton1.setOnClickListener(new View.OnClickListener() { 
 
\t \t \t 
 
\t \t \t @Override 
 
\t \t \t public void onClick(View v) { 
 
\t \t \t \t Intent i = new Intent(MainActivity.this, Team1.class); 
 
\t \t \t \t startActivity(i); 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t }); 
 
\t \t 
 
\t } 
 

 

 
}

這裏有兩個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>

回答

0

怎麼樣,如果你設置playerName.length() == 0在初始化添加按鈕之前?

這可能會阻止應用程序粉碎。