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