gpt4 book ai didi

java - Max Bench Press计算器项目不起作用

转载 作者:行者123 更新时间:2023-12-02 03:56:38 25 4
gpt4 key购买 nike

我希望每按一次按钮,文本视图就会增加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。在空对象引用上的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)
                                                                                       在java.lang.reflect.Method.invoke(本机方法)
                                                                                       在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)'
                                                                                       在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)
                                                                                       在java.lang.reflect.Method.invoke(本机方法)
                                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

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

关于java - Max Bench Press计算器项目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35387792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com