Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.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 Activity - Fatal编程技术网

无法从Android计算器应用程序中按下的按钮检索值

无法从Android计算器应用程序中按下的按钮检索值,android,android-activity,Android,Android Activity,目前正在尝试让我的简单Android计算器应用程序工作。当我尝试在模拟器上运行时,我得到: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText” 将结果返回到textview时,我也遇到了一个问题。我试过tvResult.setTextresult;但它不允许我在文本中传递浮点 我的Java: package com.example.justin.

目前正在尝试让我的简单Android计算器应用程序工作。当我尝试在模拟器上运行时,我得到: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText”

将结果返回到textview时,我也遇到了一个问题。我试过tvResult.setTextresult;但它不允许我在文本中传递浮点

我的Java:

package com.example.justin.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import javax.xml.transform.Result;


public class MainActivity extends Activity implements OnClickListener {

    EditText digit1;
    EditText digit2;
    TextView tvResult;

    Button nine, eight, seven, six, five, four, three, two, one, zero, decimal, plus, minus, divide, multiply, equals;
    String s = "0";

    char enterDigit = ' ';
    String enterOperation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //button elements
        nine = (Button) findViewById(R.id.button9);
        eight = (Button) findViewById(R.id.button8);
        seven = (Button) findViewById(R.id.button7);
        six = (Button) findViewById(R.id.button6);
        five = (Button) findViewById(R.id.button5);
        four = (Button) findViewById(R.id.button4);
        three = (Button) findViewById(R.id.button3);
        two = (Button) findViewById(R.id.button2);
        one = (Button) findViewById(R.id.button1);
        zero = (Button) findViewById(R.id.button0);
        decimal = (Button) findViewById(R.id.buttondecimal);
        plus = (Button) findViewById(R.id.buttonadd);
        minus = (Button) findViewById(R.id.buttonminus);
        divide = (Button) findViewById(R.id.buttondivide);
        multiply = (Button) findViewById(R.id.buttonmultiply);
        equals = (Button) findViewById(R.id.buttonequals);
        tvResult = (TextView) findViewById(R.id.tvResult);

        //set listeners
        nine.setOnClickListener(this);
        eight.setOnClickListener(this);
        seven.setOnClickListener(this);
        six.setOnClickListener(this);
        five.setOnClickListener(this);
        four.setOnClickListener(this);
        three.setOnClickListener(this);
        two.setOnClickListener(this);
        one.setOnClickListener(this);
        decimal.setOnClickListener(this);
        plus.setOnClickListener(this);
        minus.setOnClickListener(this);
        divide.setOnClickListener(this);
        multiply.setOnClickListener(this);
        equals.setOnClickListener(this);
        tvResult.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        float num1 = 0;
        float num2 = 0;
        float result = 0;

        num1 = Float.parseFloat(digit1.getText().toString());
        num2 = Float.parseFloat(digit2.getText().toString());

        switch (v.getId()) {
            case R.id.buttonadd:
                enterOperation = "+";
                result = num1 + num2;
                break;
            case R.id.buttonminus:
                enterOperation = "-";
                result = num1 - num2;
                break;
            case R.id.buttonmultiply:
                enterOperation = "*";
                result = num1 * num2;
                break;
            case R.id.buttondivide:
                enterOperation = "/";
                result = num1 / num2;
                break;
            default:
                break;
        }


    }
}
我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="0"
        android:gravity="right"
        android:textSize="50sp"/>"

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:onClick="enterDigit">

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="7"
            android:onClick="enterDigit"/>

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="8"
            android:onClick="enterDigit"/>

        <Button
            android:id="@+id/button9"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="9"
            android:onClick="enterDigit"/>"


        <Button
            android:id="@+id/buttondivide"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="/"
            android:onClick="enterOperation"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="4"
            android:onClick="enterDigit"/>

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="5"
            android:onClick="enterDigit"/>

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="6"
            android:onClick="enterDigit"/>


        <Button
            android:id="@+id/buttonmultiply"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="*"
            android:onClick="enterOperation"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="1"
            android:onClick="enterDigit"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="2"
            android:onClick="enterDigit"/>

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="3"
            android:onClick="enterDigit"/>


        <Button
            android:id="@+id/buttonminus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="-"
            android:onClick="enterOperation"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">

        <Button
            android:id="@+id/button0"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="0"
            android:onClick="enterDigit"/>


        <Button
            android:id="@+id/buttondecimal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="." />


        <Button
            android:id="@+id/buttonequals"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="="
            android:onClick="calculate"/>

        <Button
            android:id="@+id/buttonadd"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="+"
            android:onClick="enterOperation"/>
    </LinearLayout>
</LinearLayout>

您的EditText小部件在xml中不存在。它们需要添加到您的主线布局中的某个位置。然后,在onCreate方法中,需要通过findviewbydr.id.digit1设置它们。。。另外,去掉LinearLayout中的onClick属性;因为线性布局不可单击,所以它是无用的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal>
        <EditText
            android:id="@+id/digit1"
            android:layout_height="wrap_content"
            android:layout_width="0dip"
            android:layout_weight="1" />
        <EditText
            android:id="@+id/digit2"
            android:layout_height="wrap_content"
            android:layout_width="0dip"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="0"
        android:gravity="right"
        android:textSize="50sp"/>"

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="7"
            android:onClick="enterDigit"/>

 ...

您需要像按钮一样初始化EditText digit1和digit1 firstlg,否则会像您的问题一样导致NullPointException

@Override
public void onCreate(Bundle sIS){
    super.onCreate(sIS);
    digit1 = (EditText)findViewById(R.id.digit1);
    digit2 = (EditText)findViewById(R.id.digit2);

    ...
}