Android App Crashes on Button Click

前端 未结 4 408
野性不改
野性不改 2021-01-26 05:54

I have been attempting to make my first android application (a simple temperature converter) in Eclipse, but when I click the button on my phone the app crashes. Here is the ful

4条回答
  •  自闭症患者
    2021-01-26 06:27

    Initialize your Buttons first then set onclicklistener to them

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initialize views here 
        EditText mEdit = (EditText) findViewById(R.id.editText1);
        TextView myTextView = (TextView) findViewById(R.id.label);
        Button yourButton = (Button) findViewByid(R.id.youridforbutton);
        //set onclicklistener for your button
        yourbutton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    number = mEdit.getText().toString();
                    number2 = Integer.parseInt(number);
    
                    if (F = true) {
                        output = number2 * 9 / 5 + 32;
                    } else {
                        output = number2 - 32 * 5 / 9;
                    }
    
                    myTextView.setText("" + output);
                }
            });
    
    }
    

    Similarly set the other button also

提交回复
热议问题