Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:获取编辑文本的麻烦_Android_Android Edittext - Fatal编程技术网

Android:获取编辑文本的麻烦

Android:获取编辑文本的麻烦,android,android-edittext,Android,Android Edittext,我有一个简单的计算器应用程序的问题。如果我输入两个数字并按下按钮,应用程序总是崩溃。调试控制台说第45行有一个错误,这是 EditText number2=(EditText)findViewById(R.id.editText2) 我不明白,因为它和上面一样 活动内容如下: package com.example.taschenrechner; import android.content.Intent; import android.os.Bundle; import android.sup

我有一个简单的计算器应用程序的问题。如果我输入两个数字并按下按钮,应用程序总是崩溃。调试控制台说第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>


非常感谢

将所有这些行置于
onCreate()
方法中
setContentView()
之后。您需要在
onCreate()
时获取视图的引用,以便稍后在
活动中访问它们

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

然后在它们上设置侦听器

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

在定义android:onClick=“calc”属性时,只需为按钮单击操作实现OnClickListener。

确保使用正确的XML文件。。。。我认为这是fragment_main.xml文件完全错误堆栈