Max Bench Press Calculator项目无效

时间:2016-02-14 02:44:17

标签: java android

我希望每次按下按钮时文本视图增加2.5,但应用程序会一直崩溃。我尝试使用数字选择器,但我不喜欢它垂直定向的方式。我决定自己制作,后来我添加了长按功能,但现在我正在尝试按钮。

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

}
}

这是来自logcat的堆栈:

致命的例外:主要                                                                                    处理:com.example.luke.maxbenchcalculator,PID:2281                                                                                    java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.luke.maxbenchcalculator / com.example.luke.maxbenchcalculator.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView。 null对象引用上的setText(java.lang.CharSequence)'                                                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                        在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                        在android.os.Looper.loop(Looper.java:148)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                                                                                     引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'                                                                                        at com.example.luke.maxbenchcalculator.MainActivity.onCreate(MainActivity.java:39)                                                                                        在android.app.Activity.performCreate(Activity.java:6237)                                                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                                                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                        在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                        在android.os.Looper.loop(Looper.java:148)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:2)

您从未为textView成员分配值,因此它保持为null。然后你尝试在onCreate期间调用一个方法,这会导致崩溃。为它分配一个值,并确保它在调用方法之前不为空。