Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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
Java 简单的计算停止了,我怎样才能做得更好?_Java_Android_Button_Onclicklistener - Fatal编程技术网

Java 简单的计算停止了,我怎样才能做得更好?

Java 简单的计算停止了,我怎样才能做得更好?,java,android,button,onclicklistener,Java,Android,Button,Onclicklistener,简单的计算,每个按钮我必须定义一个值,我可以直接从按钮得到它吗? 这是一个简单的应用程序,但当我想测试它时,我得到错误“应用程序停止”。我对所有按钮使用一个侦听器,我认为这会导致错误 import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import an

简单的计算,每个按钮我必须定义一个值,我可以直接从按钮得到它吗? 这是一个简单的应用程序,但当我想测试它时,我得到错误“应用程序停止”。我对所有按钮使用一个侦听器,我认为这会导致错误

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

public class MainActivity extends Activity implements OnClickListener {

    Button buttOne, buttTwo, buttThree, buttFour, buttFive, buttSix, buttSeven, buttEight, buttNine, buttZero;
    Button buttSum, buttSubstr;
    Double result, buffer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttOne = (Button) findViewById(R.id.one);
        buttTwo = (Button) findViewById(R.id.two);
        buttThree = (Button) findViewById(R.id.three);
        buttFour = (Button) findViewById(R.id.four);
        buttFive = (Button) findViewById(R.id.five);
        buttSix = (Button) findViewById(R.id.six);
        buttSeven = (Button) findViewById(R.id.seven);
        buttEight = (Button) findViewById(R.id.eight);
        buttNine = (Button) findViewById(R.id.nine);
        buttZero = (Button) findViewById(R.id.zero);

        buttSum = (Button) findViewById(R.id.sum);
        buttSubstr = (Button) findViewById(R.id.substr);

             buttOne.setOnClickListener(this);
     buttTwo.setOnClickListener(this);
     buttThree.setOnClickListener(this);
     buttFour.setOnClickListener(this);
      buttFive.setOnClickListener(this);
      buttSix.setOnClickListener(this);
      buttSeven.setOnClickListener(this);
      buttEight.setOnClickListener(this);
      buttNine.setOnClickListener(this);
      buttZero.setOnClickListener(this); 
      buttSum.setOnClickListener(this);
      buttSubstr.setOnClickListener(this);  

        result = 0.0;
    }

    /*
     * @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 void onClick(View v) {

        switch (v.getId()) {

        case R.id.one:
            buffer = 1.0;
            break;
        case R.id.two:
            buffer = 2.0;
            break;
        case R.id.three:
            buffer = 3.0;
            break;
        case R.id.four:
            buffer = 4.0;
            break;
        case R.id.five:
            buffer = 5.0;
            break;
        case R.id.six:
            buffer = 6.0;
            break;
        case R.id.seven:
            buffer = 7.0;
            break;
        case R.id.eight:
            buffer = 8.0;
            break;
        case R.id.nine:
            buffer = 9.0;
            break;
        case R.id.zero:
            buffer = 0.0;
            break;
        case R.id.substr:
            result = -buffer;
            break;
        case R.id.sum:
            result = +buffer;
        }
    }

}
布局:

    <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:background="@style/AppTheme"
android:gravity="center_horizontal"
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=".MainActivity" >

<Button
    android:id="@+id/seven"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/seven" />

<Button
    android:id="@+id/eight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/seven"
    android:layout_alignBottom="@+id/seven"
    android:layout_toRightOf="@+id/seven"
    android:text="@string/eight" />

<Button
    android:id="@+id/nine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/eight"
    android:layout_alignBottom="@+id/eight"
    android:layout_toRightOf="@+id/eight"
    android:text="@string/nine" />

<Button
    android:id="@+id/four"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/seven"
    android:layout_below="@+id/seven"
    android:text="@string/four" />

<Button
    android:id="@+id/five"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/four"
    android:layout_alignBottom="@+id/four"
    android:layout_toRightOf="@+id/four"
    android:text="@string/five" />

<Button
    android:id="@+id/six"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/nine"
    android:layout_below="@+id/nine"
    android:text="@string/six" />

<Button
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/four"
    android:layout_below="@+id/four"
    android:text="@string/one" />

<Button
    android:id="@+id/two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/five"
    android:layout_below="@+id/five"
    android:text="@string/two" />

<Button
    android:id="@+id/three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/six"
    android:layout_below="@+id/six"
    android:text="@string/three" />

<Button
    android:id="@+id/zero"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/one"
    android:layout_toLeftOf="@+id/three"
    android:text="@string/zero" />

<Button
    android:id="@+id/division"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/nine"
    android:text="@string/division" />

<Button
    android:id="@+id/multiple"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/six"
    android:layout_alignBottom="@+id/six"
    android:layout_toRightOf="@+id/six"
    android:text="@string/multiple" />

<Button
    android:id="@+id/substr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/six"
    android:layout_toRightOf="@+id/six"
    android:text="@string/subst" />

<Button
    android:id="@+id/sum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/three"
    android:layout_toRightOf="@+id/three"
    android:text="@string/sum" />

<Button
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/sum"
    android:layout_alignBottom="@+id/sum"
    android:layout_toLeftOf="@+id/sum"
    android:text="@string/result" />

<Button
    android:id="@+id/clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/one"
    android:layout_below="@+id/one"
    android:text="@string/clear" />

<EditText
    android:id="@+id/output"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/eight"
    android:layout_alignRight="@+id/division"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/eight"
    android:layout_alignRight="@+id/division"
    android:ems="10"
    android:inputType="numberDecimal" />

您需要添加onClickListeners

buttOne.setOnClickListener(this);

等等……:)

也许是给你的。使用OcuS答案中建议的android:tag。所有按钮的
按钮在哪里。setOnClickListener(this)
?将xml布局张贴在alos上,因为findViewById可能返回Null。我添加了控制台和布局xmlI fogot。为了在更改后添加此项,我在出错时设置了所有侦听器,我更新代码你确定你还没有关闭你的亲戚吗?
buttOne.setOnClickListener(this);