-2

Background - I only started using android studio this morning, so I'm green as grass.

I'm trying to make a simple Counter app where it adds/subtract one from the total and then displays it. But for some reason the app just crashes on start up.

Here is my code - there are a couple more OnClickListener methods that I did not include, but they're all the same code.

Thanks in advance.

public class MainActivity extends AppCompatActivity {

Button counter1Up, counter1Down, counter2Down, counter2Up;
TextView counter1, counter2;

double lifeTotal1 = 20, lifeTotal2 = 20;

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


    counter1Up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            lifeTotal1 = Double.parseDouble(counter1.getText().toString());
            counter1.setText(String.valueOf(lifeTotal1 + 1));
        }
    });

    counter2Up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            lifeTotal2 = Double.parseDouble(counter2.getText().toString());
            counter2.setText(String.valueOf(lifeTotal2 + 1));
        }
    });

    counter1Down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            lifeTotal1 = Double.parseDouble(counter1.getText().toString());
            counter1.setText(String.valueOf(lifeTotal1 - 1));
        }
    });


public void setUI(){
    counter1Up = findViewById(R.id.counter2UpButton);
    counter1Down = findViewById(R.id.Counter1DownButton);
    counter2Up = findViewById(R.id.counter2UpButton);
    counter2Down = findViewById(R.id.counter2DownButton);
    counter1.setText("20");
    counter2.setText("20");
}
}

And this is what the Event Log shows:

1/19/2019
3:29 AM Gradle sync started

3:29 AM Project setup started

3:29 AM Gradle sync finished in 2 s 979 ms

3:29 AM Syncing only active variant
                You can disable this experimental feature from
                File → Settings → Experimental → Gradle → Only sync the active variant

3:29 AM Executing tasks: [:app:generateDebugSources]

3:29 AM Gradle build finished in 780 ms

3:51 AM Executing tasks: [:app:assembleDebug]

3:52 AM Gradle build finished in 13 s 75 ms
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Filipe
  • 109
  • 1
  • 6
  • 2
    Post your error logs.this is build logs.Bottom of android studio you can see buttons such as Run-Build-Terminal-Logcat etc. Click logcat And copy what it say. – uzaysan Jan 19 '19 at 12:12

2 Answers2

0

welcome to the world of Android development.

I'm not sure if you've introduced yourself to the debugging an Android application yet, but the output of the event log might not help in this case. Google does provide a ton of information on this topic, and the very first thing you should familiarize yourself with is the Android debugger and its output view in Android Studio. You may want to start learning here: https://developer.android.com/studio/debug/

Regarding your problem, I think you've ran into a classic NullPointerException in line 5 of your setUI() method where you're accessing the TextView variable counter1 without retrieving it from the layout using findViewById() just like in lines 1-4 of the very same method.

Sam del Rö
  • 354
  • 5
  • 9
  • Ahhh I knew it was gonna be something super obvious, thanks for the assistance and for the helpful link! – Filipe Jan 19 '19 at 21:22
0

You haven't initialized your textviews counter1 and counter2. Initialize both the variable before calling methods on them.