前提・実現したいこと
AndoriodStudioを利用して野球のSBOとイニングを表示するプログラムを作ろうとしています。
ストライクのボタンを押すとストライクが1つ増え、ボールのボタンを押すとボールが1つ増えるようにしたいです。
発生している問題・エラーメッセージ
ボタンを押すと別のクラスに飛んでストライクとボールが増えるようにしたは良いけれど、ボタンをいくら押してもカウントが1から増えません。
具体的には
S●
B●
O
これ以上になることが出来ません。別のクラスを利用するのが不慣れの為、宣言部分でミスが有るかもしれません。
該当のソースコード
activiti_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="139dp" android:layout_height="41dp" android:layout_marginStart="8dp" android:layout_marginTop="16dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="@string/textview" app:layout_constraintBottom_toTopOf="@+id/textView2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/textView4" android:layout_width="135dp" android:layout_height="39dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="@string/textview4" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView3" app:layout_constraintVertical_bias="0.26" /> <TextView android:id="@+id/textView3" android:layout_width="135dp" android:layout_height="39dp" android:layout_marginStart="8dp" android:layout_marginTop="12dp" android:layout_marginEnd="8dp" android:text="@string/textview3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="308dp" android:text="ストライク" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:text="ボール" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" /> <TextView android:id="@+id/textView2" android:layout_width="135dp" android:layout_height="39dp" android:layout_marginStart="8dp" android:layout_marginTop="84dp" android:layout_marginEnd="8dp" android:text="@string/textview2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.507" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
MainActivity.java package com.example.sboiningu; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private SBCount sbc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView ini = findViewById(R.id.textView); //イニング、ストライク、ボール、アウトのテキスト設定 TextView sto = findViewById(R.id.textView2); TextView bo = findViewById(R.id.textView3); TextView au = findViewById(R.id.textView4); Button stob = findViewById(R.id.button); Button bob = findViewById(R.id.button2); new SBCount(MainActivity.this,10); //初期化処理 //SBO処理開始 stob.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new SBCount(MainActivity.this,0); } }); bob.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new SBCount(MainActivity.this,1); } }); } }
SBCount.java package com.example.sboiningu; import android.widget.TextView; class SBCount { private int iningu=1,omote=1,S=0,B=0,O=0; String maru1="●",maru2="●●",maru3="●●●",maru4="●●●●"; SBCount(){ } SBCount(MainActivity mainActivity, int hantei) { TextView ini = (TextView) mainActivity.findViewById(R.id.textView); TextView sto = (TextView) mainActivity.findViewById(R.id.textView2); TextView bo = (TextView) mainActivity.findViewById(R.id.textView3); TextView au = (TextView) mainActivity.findViewById(R.id.textView4); if (hantei == 10) { //初期化 ini.setText(iningu + "回表"); sto.setText("S"); bo.setText("B"); au.setText("O"); } if (hantei == 0) { //ストライク S=S+1; switch (S) { case (0): { sto.setText("S"); break; } case (1): { sto.setText("S" + maru1); break; } case (2): { sto.setText("S" + maru2); break; } case (3): { sto.setText("S" + maru3); break; } } if (S == 3) { O = O + 1; S = 0; B = 0; } switch(O){ case(0):{ au.setText("O"); break; } case(1):{ au.setText("O"+maru1); break; } case(2):{ au.setText("O"+maru2); break; } case(3):{ au.setText("O"+maru3); break; } } if(O==3){ if(omote==1){ omote=0; O=0; }else{ omote=1; iningu=iningu+1; O=0; } } if(omote==1){ ini.setText(iningu+"回表"); }else{ ini.setText(iningu+"回裏"); } } if (hantei == 1) { B=B+1; switch (B) { case (0): { bo.setText("B"); break; } case (1): { bo.setText("B" + maru1); break; } case (2): { bo.setText("B" + maru2); break; } case (3): { bo.setText("B" + maru3); break; } case (4): { bo.setText("B" + maru4); break; } } if (B == 4) { S = 0; B = 0; } } } }
補足情報(FW/ツールのバージョンなど)
使用しているバージョンAndroidStudio3.4.1
使われていないTextViewや使われていない変数が有りますが、そこは無視してください。手間を掛けさせて申し訳有りません。
回答1件
あなたの回答
tips
プレビュー