2017-12-23 3 views
1

Ich versuche herauszufinden, warum meine App abstürzt, wenn meine AddThreeToTeamA-Schaltfläche beim Klicken auf meine Anwendung abstürzt. Ich habe meinen XML- und meinen Java-Code hinzugefügt.
Ich folge einem Tutorial aber es stürzt offensichtlich nicht ab und ich habe mehrere verschiedene Lösungen ausprobiert.
Jeder Hinweis wird geschätzt.So fügen Sie onClick für Schaltflächen hinzu?

package com.themovingmonkey.courtcounter; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.widget.TextView; 

    public class MainActivity extends AppCompatActivity { 


     int score = 0; 
     int scoreTeamA; 

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

     } 
     public void addThreeForTeamA() { 
      scoreTeamA = score + 3; 
      displayForTeamA(scoreTeamA); 
     } 

     public void addTwoForTeamA() { 
      scoreTeamA = score + 2; 
      displayForTeamA(scoreTeamA); 
     } 

     public void addOneForTeamA() { 
      scoreTeamA = score + 1; 
      displayForTeamA(scoreTeamA); 
     } 
     public void displayForTeamA(int score) { 
      TextView scoreView = (TextView) findViewById(R.id.Team_A_Score); 
      scoreView.setText(String.valueOf(scoreTeamA)); 
     } 


    } 
     //////////////////////////////////////////////////////////////// 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text= "Team A" 
     android:padding="4dp"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="0" 
     android:padding="4dp" 
     android:id="@+id/Team_A_Score"/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="3 Points" 
     android:layout_margin="8dp" 
     android:onClick="addThreeForTeamA" 
     /> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="2 Points" 
     android:layout_margin="8dp" 
     android:onClick="addTwoForTeamA" 
     /> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="1 Point" 
     android:layout_margin="8dp" 
     android:onClick="addOneForTeamA" 
     /> 
</LinearLayout> 
+1

Sie müssen die Protokolle für Ihren Absturz veröffentlichen! – Xenolion

Antwort

0

Alle mit dem Attribut android:onClick="someMethod" definierten Methoden. Dann müssen sie öffentlich und einen View als einziger Parameter übergeben sollte, ändern Sie Ihre Methoden:

public void someMethod(View view){ 

    //Everything else! 

} 

Ihre Methoden nicht View als einziger Parameter überhaupt noch passieren kann!

+0

danke ein Haufen! funktionierte wie ein Zauber, frohe Feiertage! –

+0

Okay danke und ** Happy Coding! **. – Xenolion

+0

Hey @GreyLarkan warum hast du die Annahme meiner Antwort entfernt? Funktioniert es nicht ??? – Xenolion

Verwandte Themen