按钮点击问题

发布于 2024-10-26 21:36:30 字数 2414 浏览 1 评论 0原文

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

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 组件中,您应该只能写入数字。我不知道为什么它不起作用。

I'm a beginner in application development. My problem is, that when I run my app and I click on the Calculate button, the program stops. The code:

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

In the edittext component you should be able to write numbers only. I don't know why it's not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2024-11-02 21:36:30

问题在于这两行:

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

如果您在 EditText 中仅使用数字,第一行不会失败,但最好确保或至少捕获尝试将字符转换为数字值时引发的异常。此外,您还可以使用 Integer.valueOf(strength).intValue(); ,即使它通常并不是真正必要的。

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

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

The problem are these two lines:

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

The first won't fail if you use only numbers in your EditText but it would be better to ensure that or at least catch the exception that is thrown when you try to convert a character to a numberical value. Additionally you could also use Integer.valueOf(strength).intValue(); even so it is normally not really necessary.

The real problem is the second line. You declared the variable spinner but you never instantiate it. That's why you will get a NullPointerException there.

On an unrelated note: You also should start your class name with a capital letter to follow the Java naming conventions.

筑梦 2024-11-02 21:36:30

您没有在任何地方实例化 spinner ,但您在按钮单击方法的第二行中引用它。可能是空引用问题。

You're not instantiating spinner anywhere, but you're referencing it in the second line of your button click method. Probably a null reference problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文