Question

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;...

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>

Solutions

Expert Solution

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


Related Solutions

Golfgamez is an idea for a business that sells a mobile phone app for golfers. This...
Golfgamez is an idea for a business that sells a mobile phone app for golfers. This app has three key functions: 1) GPS-enabled scoring & record-keeping, 2) facilitation of side bets between golfers, and 3) offering coupons for discounted golf merchandise from third-party retailers. This business will have three revenue streams (i.e. app purchases, in-app coupon purchases, and in-app gambling). As you develop this concept, will you stick with all three of these streams, or will you eliminate any? Are...
You have been hired by a startup that sells travel insurance through a mobile app. Develop...
You have been hired by a startup that sells travel insurance through a mobile app. Develop a strategy for strategic partnerships using the principles of cross selling, up selling, bundling and unbundling to grow the market share of the 65-90 year old customer market segment . ( Detailed Answer)
CHERCRO Inc. is a startup. It is estimated that the company will not be paying any...
CHERCRO Inc. is a startup. It is estimated that the company will not be paying any dividends for the coming 4 years. If the company distributes $3 per share 5 years from today, the growth rate of the dividends will be 2% per year going forward. If, instead the company distributes $2 per share at the 5th year, the growth rate of dividends will be 6% per year. As an investor of CHERCRO, which policy would you support if the...
Your startup company’s app-enabled ketchup and mustard dispenser uses a special plastic gear. The strength of...
Your startup company’s app-enabled ketchup and mustard dispenser uses a special plastic gear. The strength of the plastic in the gears (measured in foot-pounds) is important if the dispenser is to last through an entire basketball season. Random samples from two different suppliers are gathered and the following descriptive statistics are measured: i. Sample 1: 1. n=10 2. ???=?????? 3. ????=???? ii. Sample 2: 1. n=16 2. ???=?????? 3. ????=???? You believe that the strength of both manufacturer’s products is...
Your company makes apps for the smartphone/tablet market. Create an idea for a new app, and...
Your company makes apps for the smartphone/tablet market. Create an idea for a new app, and discuss how this new app would be introduced to your market. Choose an app for one of these markets: 1-business; 2-health; 3-education; 4-sports; or 5-shopping.
A export import business would be a great idea right now because of the state of...
A export import business would be a great idea right now because of the state of the country. The first thing would be is to make sure all the paperwork is order and really work with the right companies and want to do things the right way with the product being shipped. What are your plans to ensure all the rules are being followed?
You have come up with the idea for a new workplace productivity app which you plan...
You have come up with the idea for a new workplace productivity app which you plan to call B1NDER. To develop it, you need to make an immediate investment of $65,000. Given the large number of competing apps in the market, you are worried about how people will respond to your app. You will learn the response in exactly one year. You believe there are three possibilities: Response to app Probability Outcome Excellent 0.1 Cash flow of $100,000 every year...
Business Scenario: You have a great idea for a new mobile app. You do not have...
Business Scenario: You have a great idea for a new mobile app. You do not have any technical expertise to create one, however, nor do you have any idea on how to attract the money you need to get your idea off the ground or marketed. You are partway through your diploma at BCIT and have a couple of friends in your class who have some of the skills and connections you think you need. You talk to them at...
questions are from THE BATTLE OF IDEA, urgent, and thanks!!!! 1. Washington D.C., 1974. What industry...
questions are from THE BATTLE OF IDEA, urgent, and thanks!!!! 1. Washington D.C., 1974. What industry became the focus of efforts to deregulate?   2. Britain, 1979. What policy actions did Margaret Thatcher pursue as Britain’s new Prime Minister? 3. USA, 1979. What did Paul Volcker, Chairman of the Federal Reserve, believe to be the worst of all economic evils, circa 1979? 3. Who were the American politician and future president who trumpeted free-market principles?
please answer if you know or suggest any right info is there an app where i...
please answer if you know or suggest any right info is there an app where i can keet track of its inventory and provides me with its barcode for my items (creating) -i have a store and plan to use my phone as a bar reader and keep track of its inventory when i sell something as it will automatically adjust itself. so that way i can go to just one place which is the whole point!!! i dont care...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT