我有一个简单的计算器应用程序的问题。如果我输入两个数字并按下按钮,则应用程序始终崩溃。 debuging控制台说第45行有一个错误,就是这样
EditText number2 = (EditText) findViewById(R.id.editText2);
我不明白,因为它和上面一样。
这是活动:
package com.example.taschenrechner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_RESULT = "com.example.Taschenrechner.RESULT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calc (View view){
EditText number1 = (EditText) findViewById(R.id.editText1);
EditText number2 = (EditText) findViewById(R.id.editText2);
RadioButton addButton = (RadioButton) findViewById(R.id.radio0);
RadioButton subButton = (RadioButton) findViewById(R.id.radio1);
RadioButton mulButton = (RadioButton) findViewById(R.id.radio2);
if (number1.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number in Erste Zahl",
Toast.LENGTH_LONG).show();
return;
}
else if (number2.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number in Zweite Zahl",
Toast.LENGTH_LONG).show();
return;
}
float n1 = Float.parseFloat(number1.getText().toString());
float n2 = Float.parseFloat(number2.getText().toString());
float result;
if (addButton.isChecked()){
result = n1+n2;
}
else if (subButton.isChecked()){
result = n1-n2;
}
else if (mulButton.isChecked()){
result = n1*n2;
}
else{
if ( n2 == 0){
Toast.makeText(this, "Division durch 0 ist nicht erlaubt",
Toast.LENGTH_LONG).show();
return;
}
result = n1/n2;
}
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra(EXTRA_RESULT, result);
startActivity(intent);
}
}
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.taschenrechner.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/zahl" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:text="@string/zahl1" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/add" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sub" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mul" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/div" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/radioGroup1"
android:onClick="calc"
android:text="@string/calc" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
非常感谢!!
答案 0 :(得分:1)
只需为按钮单击操作实现OnClickListener,无论您在何处定义android:onClick =&#34; calc&#34;属性。
答案 1 :(得分:0)
在onCreate()
之后将所有这些行放在setContentView()
方法中。您需要在onCreate()
时查看视图的引用,以便稍后在activity
中访问它们上
EditText number1 = (EditText) findViewById(R.id.editText1);
EditText number2 = (EditText) findViewById(R.id.editText2);
RadioButton addButton = (RadioButton) findViewById(R.id.radio0);
RadioButton subButton = (RadioButton) findViewById(R.id.radio1);
RadioButton mulButton = (RadioButton) findViewById(R.id.radio2);
然后将听众设置在他们身上
答案 2 :(得分:0)
在buton.setOnClickListener()
方法中使用您的代码
button_to_calculate.setOnClickListener(new OnClickListener(){
if (number1.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number in Erste Zahl",
Toast.LENGTH_LONG).show();
return;
}
else if (number2.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number in Zweite Zahl",
Toast.LENGTH_LONG).show();
return;
}
float n1 = Float.parseFloat(number1.getText().toString());
float n2 = Float.parseFloat(number2.getText().toString());
float result;
if (addButton.isChecked()){
result = n1+n2;
}
else if (subButton.isChecked()){
result = n1-n2;
}
else if (mulButton.isChecked()){
result = n1*n2;
}
else{
if ( n2 == 0){
Toast.makeText(this, "Division durch 0 ist nicht erlaubt",
Toast.LENGTH_LONG).show();
return;
}
});