我是 Android 的初学者,我正在尝试创建一个简单的 Android 应用程序,它有两个文本框 (EditText) 以将两个数字作为输入。然后我正在创建具有四个单选按钮(加、减、乘、除)的单选组。然后我有一个按钮来获得结果。当我单击按钮时,我收到一个警报框,上面写着“不幸的是,SimpleApp 已停止工作”。请帮助我。
*这是我的 Java 代码 *
package com.example.simpleapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private EditText txt1;
private EditText txt2;
private String input1;
private String input2;
private Double Num1;
private Double Num2;
private String flag;
private EditText TextResult;
Double Result=0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls()
{
((Button)findViewById(R.id.button1)).setOnClickListener(this);
}
public void onradioButtonClicked(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radio0:
if(checked)
flag = "add";
//Result = Num1+Num2;
break;
case R.id.radio1:
if(checked)
flag = "sub";
//Result = Num1-Num2;
break;
case R.id.radio2:
if(checked)
flag = "prod";
//Result = Num1*Num2;
break;
case R.id.radio3:
if(checked)
flag = "div";
//Result = Num1/Num2;
break;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextResult = (EditText)findViewById(R.id.textView2);
TextResult.setText("0");
txt1 = (EditText)findViewById(R.id.editText1);
txt2 = (EditText)findViewById(R.id.editText2);
input1 = txt1.getText().toString();
input2 = txt2.getText().toString();
Num1 = Double.parseDouble(input1);
Num2 = Double.parseDouble(input2);
if(flag == "add")
Result = Num1+Num2;
else if(flag == "sub")
Result = Num1-Num2;
else if(flag == "prod")
Result = Num1*Num2;
else Result = Num1/Num2;
TextResult.setText(String.valueOf(Result));
}
}
这是我的 Activity_Main.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=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="102dp"
android:layout_marginTop="66dp"
android:orientation="vertical" >
</LinearLayout>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="34dp" >
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tableRow1"
android:layout_below="@+id/tableRow1"
android:layout_marginLeft="14dp" >
</TableRow>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tableRow1"
android:layout_alignTop="@+id/tableRow2"
android:ems="10"
android:hint="Enter Second Number"
android:inputType="number" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter First Number"
android:inputType="number" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Simple Calculator App"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="36dp" >
</TableRow>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Add"
android:onClick="onradioButtonClicked"/>
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sub"
android:onClick="onradioButtonClicked"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:onClick="onradioButtonClicked"/>
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:onClick="onradioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_below="@+id/radioGroup1"
android:text="Check"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>