-4

我在 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>
4

1 回答 1

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

    TextView tview = (TextView)findViewById(R.id.text1); tview.setText("结果是" + x+y); 设置内容视图(tview);

  3. 阅读更多关于 android 开发的基础知识

于 2012-08-01T20:01:53.663 回答