In: Computer Science
App is crashing at startup? Any idea? Thanks! import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity { private EditText teamOneText; private EditText teamTwoText; private Button selectButton; private TextView resultView; Random r = new Random(); ArrayList<EditText> editTextList = new ArrayList<>(); ArrayList<String> editTextValues = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextList.add(teamOneText); editTextList.add(teamTwoText); for(int i=0;i<editTextList.size();i++){ editTextValues.add(editTextList.get(i).getText().toString()); } teamOneText = findViewById(R.id.team_one); teamTwoText = findViewById(R.id.team_two); resultView = findViewById(R.id.result_view); selectButton = findViewById(R.id.select_button); selectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { resultView.setText(editTextValues.get(r.nextInt(editTextValues.size()))); } }); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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/result_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Result shown here" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.70" /> <Button android:id="@+id/select_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select Random Team" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.40" /> <EditText android:id="@+id/team_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Team's name" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.70" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.15" /> <EditText android:id="@+id/team_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Team's name" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.70" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.25" /> <TextView android:id="@+id/team1_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Team 1" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.15" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.16" /> <TextView android:id="@+id/team2_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Team 2" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.15" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.26" /> </androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources> <string name="app_name">TeamSelector</string> </resources>
The app is crashing at startup because there is a problem in MainActivity,java file's OnCreate() function
You are adding the EditText teamOneText and EditText teamTwoText before initialising it.
i.e.
you have written:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextList.add(teamOneText); //adding teamOneText without initialising editTextList.add(teamTwoText); //adding teamOneText without initialising for(int i=0;i<editTextList.size();i++){ editTextValues.add(editTextList.get(i).getText().toString()); } teamOneText = findViewById(R.id.team_one); //initailising after using it teamTwoText = findViewById(R.id.team_two); //initailising after using it resultView = findViewById(R.id.result_view); selectButton = findViewById(R.id.select_button); selectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { resultView.setText(editTextValues.get(r.nextInt(editTextValues.size()))); } }); }
In this onCreate function you have to initialise first then use it.
So the correct code for the MainActivity,java will be as follows:
**********************************************************************************************************************************************
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity { private EditText teamOneText; private EditText teamTwoText; private Button selectButton; private TextView resultView; Random r = new Random(); ArrayList<EditText> editTextList = new ArrayList<>(); ArrayList<String> editTextValues = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); teamOneText = findViewById(R.id.team_one); //INITIALISING THE EditTExt teamOneText teamTwoText = findViewById(R.id.team_two); //INITIALISING THE EditTExt teamTwoText editTextList.add(teamOneText); //Then using EditText teamOneText and adding to the list editTextList.add(teamTwoText); //Then using EditText teamTwoText and adding to the list for(int i=0;i<editTextList.size();i++){ editTextValues.add(editTextList.get(i).getText().toString()); } resultView = findViewById(R.id.result_view); selectButton = findViewById(R.id.select_button); selectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { resultView.setText(editTextValues.get(r.nextInt(editTextValues.size()))); } }); } }
***********************************************************************************************************************************************
**Correct this thing the app will not crash
After running this corrected code the app will start like this