不幸的是,计算器已停止。

时间:2012-08-01 19:53:54

标签: android

我没有在eclipse中遇到任何错误。 (这只是一个部分应用程序,我只编写了第一个“添加”按钮功能。)请在这里帮忙,因为我的应用程序在输入值并单击添加按钮后崩溃。 这是我的代码: -

package ajay.calculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   EditText box1 = (EditText)findViewById(R.id.box_1);
   EditText box2 = (EditText)findViewById(R.id.box_2);




}




public void addNumbers(EditText box1,EditText box2){

 double a,b; 

 String str,str2 = new String();     
 str = box1.getText().toString();
 str2 = box2.getText().toString();
 a = Double.parseDouble(str);
 b = Double.parseDouble(str2);

 Log.d(box1.getText().toString(),"TAG");
 Intent intent1 = new Intent(this,Result.class);
 intent1.putExtra("first_number", a);
 intent1.putExtra("second number", b);
 startActivity(intent1);

}

public void multiplyNumbers(EditText box1,EditText box2){

 double a,b; 

 String str,str2 = new String();     
 str = box1.getText().toString();
 str2 = box2.getText().toString();
 a = Double.parseDouble(str);
 b = Double.parseDouble(str2);
 Intent intent1 = new Intent(this,Result.class);
 intent1.putExtra("first_number", a);
 intent1.putExtra("second number", b);
 startActivity(intent1);

}

public void subtractNumbers(EditText box1,EditText box2){

 double a,b; 

 String str,str2 = new String();     
 str = box1.getText().toString();
 str2 = box2.getText().toString();
 a = Double.parseDouble(str);
 b = Double.parseDouble(str2);
 Intent intent1 = new Intent(this,Result.class);
 intent1.putExtra("first_number", a);
 intent1.putExtra("second number", b);
 startActivity(intent1);
}

public void divideNumbers(EditText box1,EditText box2){

 double a,b; 

 String str,str2 = new String();     
 str = box1.getText().toString();
 str2 = box2.getText().toString();
 a = Double.parseDouble(str);
 b = Double.parseDouble(str2);
 Intent intent1 = new Intent(this,Result.class);
 intent1.putExtra("first_number", a);
 intent1.putExtra("second number", b);
 startActivity(intent1);
}









}


and the second activity is as follows:-


package ajay.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class Result extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent1 = getIntent();
    double x = intent1.getDoubleExtra("first_number", 0.0);
    double y = intent1.getDoubleExtra("second number", 0.0);
    TextView tview = (TextView)findViewById(R.id.text1);
    tview.setText("The result is " + x+y);
    setContentView(tview);

}
}

我的xml文件: -

1)主要

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

<EditText
    android:id="@+id/box_1"
    android:hint="@string/enter_no"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:inputType="numberDecimal" 
    >


</EditText>

<EditText
    android:id="@+id/box_2"
    android:hint="@string/enter_no"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/box_1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="numberDecimal" 

    />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/box_2"
    android:layout_below="@+id/box_2"
    android:layout_marginTop="47dp"
    android:text="@string/func_add"
    android:onClick="addNumbers" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignRight="@+id/box_2"
    android:text="@string/func_mult"
    android:onClick="multiplyNumbers" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="56dp"
    android:text="@string/func_divide"
    android:onClick="divideNumbers" />

 <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button3"
    android:layout_alignBottom="@+id/button3"
    android:layout_alignRight="@+id/button2"
    android:text="@string/func_subtract" 
    android:onClick="subtractNumbers"/>

 <TextView 
     android:id="@+id/text1"
     android:hint="@string/result"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"


     />

</RelativeLayout>

2)第二个活动的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" >

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    tools:context=".Result" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  1. 按钮可以将参数传递给addNumbers,因为没有'知道传递什么。
  2. 首先你可以调用setConetView,然后调用findViewById。

    TextView tview =(TextView)findViewById(R.id.text1); tview.setText(“结果是”+ x + y); 的setContentView(tview);

  3. 详细了解Android开发的基础知识