I want the text view to increase by 2.5 every button press, but the app keeps crashing. I tried using the number picker, but I didn't like the way it was oriented vertically. I decided to make my own and later I add the long press capabilities, but now I am experimenting with the buttons.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements OnClickListener {
private Button Calculate;
private Button plus;
private Button menus;
private OnClickListener buttonclick;
private TextView textView;
int startweight;
double weight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menus = (Button) findViewById(R.id.button3);
plus = (Button) findViewById(R.id.button2);
Calculate = (Button) findViewById(R.id.button);
menus.setOnClickListener(this);
plus.setOnClickListener(this);
Calculate.setOnClickListener(this);
int startweight = 100;
textView.setText("" + startweight );
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
if (weight != 100) {
weight = weight +2.5;
}
else{
weight = startweight - 2.5;
}
textView.setText("" + weight);
break;
case R.id.button3:
if (weight != 100) {
weight = weight - 2.5;
}
else{
weight = startweight -2.5;
}
textView.setText("" + weight);
break;
case R.id.button:
break;
}
}
}
This is the stack from the logcat:
FATAL EXCEPTION: main Process: com.example.luke.maxbenchcalculator, PID: 2281 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luke.maxbenchcalculator/com.example.luke.maxbenchcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.luke.maxbenchcalculator.MainActivity.onCreate(MainActivity.java:39) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)