为什么这段代码不起作用?

时间:2014-11-12 17:17:34

标签: java android xml

我在Android上制作了一个非常简单的计算器。它在eclipse上没有显示错误,但是当我在我的Android设备上运行时,它会停止点击按钮。

这是我的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.simplecalculator.MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:hint="@string/enter_a_no"
        android:inputType="numberSigned|numberDecimal"
        />

        <requestFocus />
    

    <Button
        android:id="@+id/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/plus"
        android:layout_alignBottom="@+id/plus"
        android:layout_toRightOf="@+id/plus"
        android:text="@string/minus"
        android:onClick="minusClick" />

    <Button
        android:id="@+id/multiply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/plus"
        android:layout_below="@+id/plus"
        android:text="@string/multiply" 
        android:onClick="multiplyClick"/>

    <Button
        android:id="@+id/divide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/multiply"
        android:layout_alignBottom="@+id/multiply"
        android:layout_alignLeft="@+id/minus"
        android:text="@string/divide"
        android:onClick="divideClick" />

    <Button
        android:id="@+id/equals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/divide"
        android:layout_alignTop="@+id/minus"
        android:layout_toRightOf="@+id/minus"
        android:onClick="equalClick"
        android:text="@string/equals" />

    <Button
        android:id="@+id/_1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/plus"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp"
        android:text="@string/_1" 
        android:onClick="_1click"/>

    <Button
        android:id="@+id/_2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/_1"
        android:layout_alignBottom="@+id/_1"
        android:layout_toRightOf="@+id/_1"
        android:text="@string/_2" 
        android:onClick="_2click"/>

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/divide"
        android:layout_alignBottom="@+id/divide"
        android:layout_toRightOf="@+id/divide"
        android:onClick="clearClick"
        android:text="@string/ce" />

    <Button
        android:id="@+id/plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/_1"
        android:layout_marginLeft="33dp"
        android:layout_marginTop="127dp"
        android:hint="@string/plus"
        android:onClick="plusClick"
        android:text="@string/plus" />

    <Button
        android:id="@+id/_4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/_1"
        android:layout_toLeftOf="@+id/_2"
        android:text="@string/_4" 
        android:onClick="_4click"/>

    <Button
        android:id="@+id/_5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/_4"
        android:layout_alignBottom="@+id/_4"
        android:layout_alignLeft="@+id/_2"
        android:text="@string/_5"
        android:onClick="_5click" />

    <Button
        android:id="@+id/_6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/_5"
        android:layout_alignBottom="@+id/_5"
        android:layout_toRightOf="@+id/_5"
        android:text="@string/_6" 
        android:onClick="_6click"/>

    <Button
        android:id="@+id/_7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/_4"
        android:layout_toLeftOf="@+id/_5"
        android:text="@string/_7" 
        android:onClick="_7click"/>

    <Button
        android:id="@+id/_8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/_5"
        android:layout_toLeftOf="@+id/_6"
        android:text="@string/_8" 
        android:onClick="_8click"/>

    <Button
        android:id="@+id/_9"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/_5"
        android:layout_toRightOf="@+id/_5"
        android:text="@string/_9" 
        android:onClick="_9click"/>

    <Button
        android:id="@+id/_3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/_5"
        android:layout_toRightOf="@+id/_2"
        android:text="@string/_3" 
        android:onClick="_3click"/>

    <Button
        android:id="@+id/_0"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/_6"
        android:layout_toRightOf="@+id/_6"
        android:text="@string/_0" 
        android:onClick="_0click"/>

</RelativeLayout>

这是我的Java代码:

package com.example.simplecalculator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {
  private double vrble;
  private double rslt;
  private double vrble2;
  String text;
  EditText etext;
  Button plus;
  Button minus;
  Button multiply;
  Button divide;
  Button equal;

  @
  Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
      vrble = 0;
      rslt = 0;
      etext = (EditText) findViewById(R.id.editText1);
      plus = (Button) findViewById(R.id.plus);
      minus = (Button) findViewById(R.id.minus);
      multiply = (Button) findViewById(R.id.multiply);
      divide = (Button) findViewById(R.id.divide);
      equal = (Button) findViewById(R.id.equals);
    } else {
      vrble = 0;
      rslt = 0;
      vrble2 = 0;
      etext = (EditText) findViewById(R.id.editText1);
      plus = (Button) findViewById(R.id.plus);
      minus = (Button) findViewById(R.id.minus);
      multiply = (Button) findViewById(R.id.multiply);
      divide = (Button) findViewById(R.id.divide);
      equal = (Button) findViewById(R.id.equals);



    }

  }

  public void _0click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "0");
  }
  public void _1click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "1");
  }
  public void _2click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "2");
  }
  public void _3click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "3");
  }
  public void _4click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "4");
  }
  public void _5click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "5");
  }
  public void _6click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "6");
  }
  public void _7click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "7");
  }
  public void _8click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "8");
  }
  public void _9click(View v) {
    text = etext.getText().toString();
    etext.setText(text + "9");
  }

  public void plusClick(View v) {
    vrble = Double.parseDouble(etext.getText().toString());
    etext.setText("");
    vrble2 = Double.parseDouble(etext.getText().toString());
    rslt = vrble + vrble2;
  }
  public void minusClick(View v) {
    vrble = Double.parseDouble(etext.getText().toString());
    etext.setText("");
    vrble2 = Double.parseDouble(etext.getText().toString());
    rslt = vrble - vrble2;
  }
  public void multiplyClick(View v) {
    vrble = Double.parseDouble(etext.getText().toString());
    etext.setText("");
    vrble2 = Double.parseDouble(etext.getText().toString());
    rslt = vrble * vrble2;
  }
  public void divideClick(View v) {
    vrble = Double.parseDouble(etext.getText().toString());
    etext.setText("");
    vrble2 = Double.parseDouble(etext.getText().toString());
    rslt = vrble / vrble2;
  }
  public void clearClick(View v) {
    vrble = 0;
    rslt = 0;
    etext.setText("0");
  }
  public void equalClick(View v) {
    etext.setText("" + rslt);
  }@
  Override
  public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @
  Override
  public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

我该怎么办?!!?!

1 个答案:

答案 0 :(得分:0)

我可以提出的一个问题是你可能得到的NumberFormatException。请参阅plusClick()

等任何方法中的这些行
vrble = Double.parseDouble(etext.getText().toString());
etext.setText(""); //setting EditText to empty
vrble2 = Double.parseDouble(etext.getText().toString()); // parsing the empty string to Double

您正在将eText设置为"",然后在下一行中将其解析为Double。这是抛出错误。您可能需要清除所有操作的登录信息,例如加减等等。

更多信息,您的堆栈跟踪会很有帮助。