ANDROID:当我点击一个按钮时,它会强行关闭

时间:2014-04-15 10:02:22

标签: java android

我正在尝试创建一个程序,它将输入2个不同的数字,并具有按钮功能总和,平均值,产品并清除值....

截至目前,我已经完成了总和和产品,但是当我测试我的程序时,它会强制关闭。请帮助我..

MyFragmentMain.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:background="@drawable/design"
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.coverttoinches.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="30dp"
    android:text="@string/i"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/txtfirstnum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/btnAdd"
    android:ems="10"
    android:hint="Enter First Number"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView02"
    android:layout_below="@+id/txtsecondnum"
    android:layout_marginTop="29dp"
    android:text="ANSWER"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="25dp"
    android:text="@string/number2"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtanswer"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:onClick="onClick"
    android:text="Addition" />

<Button
    android:id="@+id/btnprod"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnAdd"
    android:layout_alignBottom="@+id/btnAdd"
    android:layout_alignLeft="@+id/TextView01"
    android:onClick="onClick"
    android:text="Product" />

<Button
    android:id="@+id/btnave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnAdd"
    android:layout_alignBottom="@+id/btnAdd"
    android:layout_alignRight="@+id/txtanswer"
    android:onClick="onClick"
    android:text="Average" />

<Button
    android:id="@+id/btnclear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnAdd"
    android:layout_alignRight="@+id/btnAdd"
    android:layout_below="@+id/btnAdd"
    android:layout_marginTop="14dp"
     android:onClick="onClick"
    android:text="Clear" />

<EditText
    android:id="@+id/txtsecondnum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView02"
    android:layout_alignBottom="@+id/TextView02"
    android:layout_alignLeft="@+id/txtfirstnum"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:hint="Enter Second Number"
    android:inputType="number" />

<EditText
    android:id="@+id/txtanswer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextView01"
    android:layout_alignLeft="@+id/txtsecondnum"
    android:ems="10"
    android:inputType="number" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginRight="15dp"
    android:layout_toLeftOf="@+id/txtfirstnum"
    android:text="@string/number1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 </RelativeLayout>

我的MainActivity.java

    package com.example.activity2;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
EditText editFirst,editSecond,editAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);

editFirst =  (EditText) findViewById(R.id.txtfirstnum);
editSecond = (EditText) findViewById(R.id. txtsecondnum);
editAnswer = (EditText) findViewById(R.id.txtanswer);

Button btnAdd =  (Button) findViewById(R.id.btnAdd);
Button btnprod =  (Button) findViewById(R.id.btnprod);
Button btnClear = (Button) findViewById(R.id.btnclear);
Button btnAvez = (Button) findViewById(R.id.btnave);

btnAdd.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    double first= 0;
    double second=0;
    double sagot= 0;
    double sagot2 =0;   

        first=Double.parseDouble(editFirst.getText().toString());
        second=Double.parseDouble(editSecond.getText().toString());
        sagot= first + second;
        editAnswer.setText(String.valueOf(sagot));

    }
    });

    btnprod.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    double first= 0;
    double second=0;
    double sagot= 0;
    double sagot2 =0;   

        first=Double.parseDouble(editFirst.getText().toString());
        second=Double.parseDouble(editSecond.getText().toString());
        sagot2= first * second;
         editAnswer.setText(String.valueOf(sagot2));

    }
});

    btnClear.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    editFirst.setText(String.valueOf(""));
    editSecond.setText(String.valueOf(""));
    editAnswer.setText(String.valueOf(""));

    }
    });

    btnAvez.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
         double first= 0;
         double second=0;
         double sagot= 0;
         double sagot2 =0;   

             first=Double.parseDouble(editFirst.getText().toString());
             second=Double.parseDouble(editSecond.getText().toString());
             sagot= first + second;
             sagot2=sagot/2;
         editAnswer.setText(String.valueOf(sagot2));

    }
});

logcat的:

04-15 07:02:45.887: E/AndroidRuntime(3223): FATAL EXCEPTION: main
04-15 07:02:45.887: E/AndroidRuntime(3223): Process: com.example.activity2, PID: 3223
04-15 07:02:45.887: E/AndroidRuntime(3223): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity2/com.example.activity2.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.os.Looper.loop(Looper.java:136)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at dalvik.system.NativeStart.main(Native Method)
04-15 07:02:45.887: E/AndroidRuntime(3223): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:02:45.887: E/AndroidRuntime(3223):     at com.example.activity2.MainActivity.onCreate(MainActivity.java:17)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.Activity.performCreate(Activity.java:5231)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 07:02:45.887: E/AndroidRuntime(3223):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 07:02:45.887: E/AndroidRuntime(3223):     ... 11 more
04-15 07:07:46.287: I/Process(3223): Sending signal. PID: 3223 SIG: 9
04-15 07:09:39.007: D/AndroidRuntime(3269): Shutting down VM
04-15 07:09:39.007: W/dalvikvm(3269): threadid=1: thread exiting with uncaught exception (group=0xb2aadba8)
04-15 07:09:39.077: D/dalvikvm(3269): GC_FOR_ALLOC freed 88K, 6% free 3099K/3276K, paused 44ms, total 46ms
04-15 07:09:39.077: E/AndroidRuntime(3269): FATAL EXCEPTION: main
04-15 07:09:39.077: E/AndroidRuntime(3269): Process: com.example.activity2, PID: 3269
04-15 07:09:39.077: E/AndroidRuntime(3269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity2/com.example.activity2.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.os.Looper.loop(Looper.java:136)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at dalvik.system.NativeStart.main(Native Method)
04-15 07:09:39.077: E/AndroidRuntime(3269): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:09:39.077: E/AndroidRuntime(3269):     at com.example.activity2.MainActivity.onCreate(MainActivity.java:17)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.Activity.performCreate(Activity.java:5231)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 07:09:39.077: E/AndroidRuntime(3269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 07:09:39.077: E/AndroidRuntime(3269):     ... 11 more
04-15 07:13:11.577: D/AndroidRuntime(3308): Shutting down VM
04-15 07:13:11.577: W/dalvikvm(3308): threadid=1: thread exiting with uncaught exception (group=0xb2aadba8)
04-15 07:13:11.637: D/dalvikvm(3308): GC_FOR_ALLOC freed 89K, 6% free 3098K/3276K, paused 35ms, total 38ms
04-15 07:13:11.647: E/AndroidRuntime(3308): FATAL EXCEPTION: main
04-15 07:13:11.647: E/AndroidRuntime(3308): Process: com.example.activity2, PID: 3308
04-15 07:13:11.647: E/AndroidRuntime(3308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity2/com.example.activity2.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.os.Looper.loop(Looper.java:136)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at dalvik.system.NativeStart.main(Native Method)
04-15 07:13:11.647: E/AndroidRuntime(3308): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:13:11.647: E/AndroidRuntime(3308):     at com.example.activity2.MainActivity.onCreate(MainActivity.java:17)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.Activity.performCreate(Activity.java:5231)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 07:13:11.647: E/AndroidRuntime(3308):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 07:13:11.647: E/AndroidRuntime(3308):     ... 11 more
04-15 07:13:14.457: I/Process(3308): Sending signal. PID: 3308 SIG: 9
04-15 07:24:33.137: D/AndroidRuntime(3348): Shutting down VM
04-15 07:24:33.137: W/dalvikvm(3348): threadid=1: thread exiting with uncaught exception (group=0xb2aadba8)
04-15 07:24:33.197: D/dalvikvm(3348): GC_FOR_ALLOC freed 90K, 6% free 3097K/3276K, paused 44ms, total 46ms
04-15 07:24:33.197: E/AndroidRuntime(3348): FATAL EXCEPTION: main
04-15 07:24:33.197: E/AndroidRuntime(3348): Process: com.example.activity2, PID: 3348
04-15 07:24:33.197: E/AndroidRuntime(3348): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity2/com.example.activity2.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.os.Looper.loop(Looper.java:136)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at dalvik.system.NativeStart.main(Native Method)
04-15 07:24:33.197: E/AndroidRuntime(3348): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:24:33.197: E/AndroidRuntime(3348):     at com.example.activity2.MainActivity.onCreate(MainActivity.java:17)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.Activity.performCreate(Activity.java:5231)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 07:24:33.197: E/AndroidRuntime(3348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 07:24:33.197: E/AndroidRuntime(3348):     ... 11 more
04-15 07:26:02.637: D/AndroidRuntime(3388): Shutting down VM
04-15 07:26:02.637: W/dalvikvm(3388): threadid=1: thread exiting with uncaught exception (group=0xb2aadba8)
04-15 07:26:02.697: D/dalvikvm(3388): GC_FOR_ALLOC freed 93K, 6% free 3094K/3276K, paused 52ms, total 54ms
04-15 07:26:02.707: E/AndroidRuntime(3388): FATAL EXCEPTION: main
04-15 07:26:02.707: E/AndroidRuntime(3388): Process: com.example.activity2, PID: 3388
04-15 07:26:02.707: E/AndroidRuntime(3388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity2/com.example.activity2.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.os.Looper.loop(Looper.java:136)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at dalvik.system.NativeStart.main(Native Method)
04-15 07:26:02.707: E/AndroidRuntime(3388): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:26:02.707: E/AndroidRuntime(3388):     at com.example.activity2.MainActivity.onCreate(MainActivity.java:17)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.Activity.performCreate(Activity.java:5231)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 07:26:02.707: E/AndroidRuntime(3388):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 07:26:02.707: E/AndroidRuntime(3388):     ... 11 more
04-15 07:26:04.977: I/Process(3388): Sending signal. PID: 3388 SIG: 9
04-15 07:31:52.157: D/AndroidRuntime(3437): Shutting down VM
04-15 07:31:52.157: W/dalvikvm(3437): threadid=1: thread exiting with uncaught exception (group=0xb2aadba8)
04-15 07:31:52.207: D/dalvikvm(3437): GC_FOR_ALLOC freed 96K, 6% free 3092K/3276K, paused 39ms, total 40ms
04-15 07:31:52.207: E/AndroidRuntime(3437): FATAL EXCEPTION: main
04-15 07:31:52.207: E/AndroidRuntime(3437): Process: com.example.activity2, PID: 3437
04-15 07:31:52.207: E/AndroidRuntime(3437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity2/com.example.activity2.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.os.Looper.loop(Looper.java:136)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at dalvik.system.NativeStart.main(Native Method)
04-15 07:31:52.207: E/AndroidRuntime(3437): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
04-15 07:31:52.207: E/AndroidRuntime(3437):     at com.example.activity2.MainActivity.onCreate(MainActivity.java:17)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.Activity.performCreate(Activity.java:5231)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 07:31:52.207: E/AndroidRuntime(3437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 07:31:52.207: E/AndroidRuntime(3437):     ... 11 more
04-15 07:31:56.397: I/Process(3437): Sending signal. PID: 3437 SIG: 9

2 个答案:

答案 0 :(得分:1)

你的RelativeLayout和你的按钮&#34;平均&#34;具有相同的ID android:id =&#34; @ + id / btnave&#34;

更改其中一个并清理项目。

答案 1 :(得分:-1)

试试这个..

同时从android:onClick="onClick"btnAdd

中删除btnprod``Button
public class MainActivity extends ActionBarActivity {
EditText editFirst,editSecond,editAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    editFirst =  (EditText) findViewById(R.id.txtfirstnum);
   editSecond = (EditText) findViewById(R.id. txtsecondnum);
   editAnswer = (EditText) findViewById(R.id.txtanswer);

    Button btnAdd =  (Button) findViewById(R.id.btnAdd);
    Button btnprod =  (Button) findViewById(R.id.btnprod);

    btnAdd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        double first= 0;
        double second=0;
        double sagot= 0;
        double sagot2 =0;   

            first=Double.parseDouble(editFirst.getText().toString());
            second=Double.parseDouble(editSecond.getText().toString());
            sagot= first + second;
            editAnswer.setText(String.valueOf(sagot));

        }
    });

    btnprod.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        double first= 0;
        double second=0;
        double sagot= 0;
        double sagot2 =0;   

            first=Double.parseDouble(editFirst.getText().toString());
            second=Double.parseDouble(editSecond.getText().toString());
            sagot2= first * second;
        editAnswer.setText(String.valueOf(sagot2));

        }
    });

}

修改

从主android:id="@+id/btnave"

中删除RelativeLayout