不幸的是,App已停止工作

时间:2014-10-04 12:05:19

标签: java android eclipse

我正在制作一个将十进制转换为二进制,十六进制和八进制的程序。它很好,直到我能够将十进制转换为二进制,但当我把方法放在如何将十进制转换为十六进制和八进制时,它遇到错误"不幸的是,应用程序已停止工作"。我重新启动了Eclipse和Emulator,但它仍然会出现同样的错误。

这是log cat的错误

10-04 07:58:32.851: D/AndroidRuntime(1026): Shutting down VM
10-04 07:58:32.861: W/dalvikvm(1026): threadid=1: thread exiting with uncaught exception (group=0xb3a97b90)
10-04 07:58:32.881: E/AndroidRuntime(1026): FATAL EXCEPTION: main
10-04 07:58:32.881: E/AndroidRuntime(1026): Process: com.example.final_project_ict_berja, PID: 1026
10-04 07:58:32.881: E/AndroidRuntime(1026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.final_project_ict_berja/com.example.final_project_ict_berja.MainActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.os.Looper.loop(Looper.java:137)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.main(ActivityThread.java:4998)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at java.lang.reflect.Method.invoke(Method.java:515)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at dalvik.system.NativeStart.main(Native Method)
10-04 07:58:32.881: E/AndroidRuntime(1026): Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
10-04 07:58:32.881: E/AndroidRuntime(1026):     at com.example.final_project_ict_berja.MainActivity.onCreate(MainActivity.java:27)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.Activity.performCreate(Activity.java:5243)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-04 07:58:32.881: E/AndroidRuntime(1026):     ... 11 more
10-04 07:58:32.861: W/dalvikvm(1026): threadid=1: thread exiting with uncaught exception (group=0xb3a97b90)
10-04 07:58:32.881: E/AndroidRuntime(1026): FATAL EXCEPTION: main
10-04 07:58:32.881: E/AndroidRuntime(1026): Process: com.example.final_project_ict_berja, PID: 1026
10-04 07:58:32.881: E/AndroidRuntime(1026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.final_project_ict_berja/com.example.final_project_ict_berja.MainActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.os.Looper.loop(Looper.java:137)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.main(ActivityThread.java:4998)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at java.lang.reflect.Method.invoke(Method.java:515)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at dalvik.system.NativeStart.main(Native Method)
10-04 07:58:32.881: E/AndroidRuntime(1026): Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
10-04 07:58:32.881: E/AndroidRuntime(1026):     at com.example.final_project_ict_berja.MainActivity.onCreate(MainActivity.java:27)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.Activity.performCreate(Activity.java:5243)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-04 07:58:32.881: E/AndroidRuntime(1026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-04 07:58:32.881: E/AndroidRuntime(1026):     ... 11 more
10-04 07:58:38.261: I/Process(1026): Sending signal. PID: 1026 SIG: 9

这是代码

package com.example.final_project_ict_berja;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

Button Binary;
Button Octal;
Button Hexadecimal;
Button Clear;
TextView Copyright;
TextView Title;
EditText Input;
EditText Output;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Binary = (Button)findViewById(R.id.Binary);
    Octal = (Button)findViewById(R.id.Octal);
    Hexadecimal = (Button)findViewById(R.id.Hexadecimal);
    Clear = (Button)findViewById(R.id.Clear);
    Copyright = (TextView)findViewById(R.id.Copyright);
    Title = (TextView)findViewById(R.id.Title);
    Input = (EditText)findViewById(R.id.Input);
    Output = (EditText)findViewById(R.id.Output);

    Binary.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //TODO Auto - Generated method stub
            int first = Integer.parseInt(Input.getText().toString());
            String bin = Integer.toBinaryString(first);
            Output.setText(bin);

        }
    });

    Hexadecimal.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //TODO Auto - Generated method stub
            int first = Integer.parseInt(Input.getText().toString());
            String hex = Integer.toHexString(first);
            Output.setText(hex);
        }
    });

    Octal.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //TODO Auto - Generated method stub
            int first = Integer.parseInt(Input.getText().toString());
            String octal = Integer.toOctalString(first);
            Output.setText(octal);
        }
    });



    Clear.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //TODO Auto - Generated method stub
            Input.setText("");
            Output.setText("");
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

}

1 个答案:

答案 0 :(得分:0)

android.widget.EditText cannot be cast to android.widget.Button

问题出在第27行的MainActivity.java中。R.id.Binary xml文件中可能R.layout.activity_main声明为<EditText>而不是<Button>