简体   繁体   中英

Android: Trouble with getting EditText

i have a problem with a simple calculator app. If I enter two numbers and press the button the app always crashes. The debuging console says there's an error in line 45, which is EditText number2 = (EditText) findViewById(R.id.editText2);

I don't get it, because it's the same line as above.

Here's the Activity:

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


}
}

And the XML-File:

<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>

Thanks a lot!!

只要定义了android:onClick =“ calc”属性,就只需为按钮点击操作实现OnClickListener即可。

Place all these lines in onCreate() method after setContentView() .You need to take the references of the views at the time of onCreate() to access them in your activity later on

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

and then set your listeners on them

use your code in buton.setOnClickListener() method like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM