android - 按钮点击问题

标签 android button click android-edittext

我是应用程序开发的初学者。我的问题是,当我运行我的应用程序并单击“计算”按钮时,程序停止了。代码:

public class screen1 extends Activity {
    private EditText name; 
    private CheckBox box1;
    private Spinner spinner;
    private TextView text1, text2, text3, text4, text5, text6;
    private Button calcbutton, closebutton;
    String strength;  

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.military_ranks , android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hubSpinner.setAdapter(adapter);

        name = (EditText)findViewById(R.id.editText1);      
        strength = name.getText().toString();  

        box1 = (CheckBox)findViewById(R.id.checkBox1);

        text1 = (TextView)findViewById(R.id.textView4);
        text2 = (TextView)findViewById(R.id.textView6);
        text3 = (TextView)findViewById(R.id.textView8);
        text4 = (TextView)findViewById(R.id.textView10);
        text5 = (TextView)findViewById(R.id.textView12);
        text6 = (TextView)findViewById(R.id.textView14);

        final Button calcbutton = (Button) findViewById(R.id.button1);
        calcbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int str = Integer.valueOf(strength);
                int rank = spinner.getSelectedItemPosition()+1;
                double sebzes;
                if(box1.isChecked()){
                    sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1*(1+1/100);
                    text1.setText(Double.toString(sebzes));
                }
                else{
                    sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1;
                    text1.setText(Double.toString(sebzes));
                }
            }
        });

        final Button closebutton = (Button) findViewById(R.id.button2);
        closebutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }
}

edittext 组件中,您应该只能写入数字。我不知道为什么它不起作用。

最佳答案

问题是这两行:

 int str = Integer.valueOf(strength);
 int rank = spinner.getSelectedItemPosition()+1;

如果您在 EditText 中只使用数字,第一个方法不会失败,但最好确保或至少捕获当您尝试将字符转换为数字值时抛出的异常。此外,您还可以使用 Integer.valueOf(strength).intValue();,即使这样通常也不是必需的。

真正的问题是第二行。您声明了变量 spinner 但从未实例化它。这就是为什么您会在那里得到 NullPointerException。

关于一个不相关的说明:您还应该以大写字母开头您的类名以遵循 Java 命名约定。

关于android - 按钮点击问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5465998/

相关文章:

android - 在 Android 6.0 Marshmallow (API 23) 上不推荐使用 getColor(int id)

android - 将 OpenBR 编译为 ArmV7 - Android 工具链问题

android - ionic cordova为Android编译,使minSdkVersion的错误小于声明的版本

java - 如何在android中搜索字符串数组?

c# - WPF中使用样式的透明按钮背景

java - Android:如何通过按钮启动 Activity ?

javascript - "Pointless button"不会更改文本

javascript - 单击时突出显示表中所有具有相同数据的行

css - 单击时更改链接的 Css

Python Selenium 创建循环以单击页面上的链接并在每个新页面上按下按钮