我是Android的初学者,我正在尝试创建一个简单的Android应用 它有两个文本框(EditText),可以将两个数字作为输入。然后我正在创建具有四个单选按钮(添加,子,乘,除)的无线电组。 然后我有一个按钮来获得结果。我收到一个AlertBox说“不幸的是,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>
答案 0 :(得分:0)
您在EditText中使用Typecasted TextView。
因此Android上升了ClassCastException
08-03 17:08:30.415:E / AndroidRuntime(7702):java.lang.ClassCastException:android.widget.TextView
private EditText TextResult;
TextResult = (EditText)findViewById(R.id.textView2);
将这些行改为
private TextView TextResult;
TextResult = (TextView) findViewById(R.id.textView2);
答案 1 :(得分:0)
您的if语句语法错误。试试这个:
if(flag == "add"){
Result = Num1+Num2;
}else if(flag == "sub"){
Result = Num1-Num2;
}else if(flag == "prod"){
Result = Num1*Num2;
}else{
Result = Num1/Num2;
}
答案 2 :(得分:0)
尝试这个...点击计算以及单选按钮检查更改
public class MainActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
private EditText txt1;
private EditText txt2;
// private String input1;
// private String input2;
private Double Num1;
private Double Num2;
// private String flag;
private TextView TextResult;
Double Result = 0.0;
RadioButton radio0, radio1, radio2, radio3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
findViewById(R.id.button1).setOnClickListener(this);
radio0 = (RadioButton) findViewById(R.id.radio0);
radio0.setOnCheckedChangeListener(this);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio1.setOnCheckedChangeListener(this);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio2.setOnCheckedChangeListener(this);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio3.setOnCheckedChangeListener(this);
TextResult = (TextView) findViewById(R.id.textView2);
TextResult.setText("0");
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
}
public void onradioButtonClicked(View view) {
}
@Override
public void onClick(View v) {
getCalculationValue();
// input1 = txt1.getText().toString();
// input2 = txt2.getText().toString();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getCalculationValue();
}
private void getCalculationValue() {
Num1 = Double.parseDouble(txt1.getText().toString().trim());
Num2 = Double.parseDouble(txt2.getText().toString().trim());
if (radio0.isChecked())
Result = Num1 + Num2;
else if (radio1.isChecked())
Result = Num1 - Num2;
else if (radio2.isChecked())
Result = Num1 * Num2;
else if (radio3.isChecked())
Result = Num1 / Num2;
TextResult.setText(String.valueOf(Result));
}
}