我的代码给出了运行时错误!我是Android编程新手,任何人都可以告诉我的错误

时间:2013-07-13 03:42:32

标签: android

有谁能解释我的android程序中的错误是什么?

Logcat:

07-13 09:04:18.429: D/AndroidRuntime(274): Shutting down VM

07-13 09:04:18.429: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

07-13 09:04:18.469: E/AndroidRuntime(274): FATAL EXCEPTION: main

07-13 09:04:18.469: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstapplication1/com.example.firstapplication1.StartingPoint}: java.lang.ClassCastException: android.widget.TextView

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)

07-13 09:04:18.469: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)

07-13 09:04:18.469: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)

07-13 09:04:18.469: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

07-13 09:04:18.469: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

07-13 09:04:18.469: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)

07-13 09:04:18.469: E/AndroidRuntime(274): Caused by: java.lang.ClassCastException: android.widget.TextView

07-13 09:04:18.469: E/AndroidRuntime(274):  at com.example.firstapplication1.StartingPoint.onCreate(StartingPoint.java:25)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

07-13 09:04:18.469: E/AndroidRuntime(274):  ... 11 more
07-13 09:04:28.659: I/Process(274): Sending signal. PID: 274 SIG: 9

StartingPoint.java:

public class StartingPoint extends Activity {
int counter;
Button add;
Button sub;
TextView display;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    counter=0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (Button) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            counter++;
            display.setText("Your Total is " + counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            counter--;
            display.setText("Your Total is " + counter);
        }
    });

}


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

 }

5 个答案:

答案 0 :(得分:1)

您收到ClassCastException因为您将TextView投放到Button这里:

display = (Button) findViewById(R.id.tvDisplay);

您需要将其投射到TextView

display = (TextView) findViewById(R.id.tvDisplay);

答案 1 :(得分:0)

更改此

      display = (Button) findViewById(R.id.tvDisplay);
     // casting to textview to button. wrong

       display = (TextView) findViewById(R.id.tvDisplay);

Caused by: java.lang.ClassCastException: android.widget.TextView

您的展示位置为TextView,初始化时您使用Button

进行展示

答案 2 :(得分:0)

更改代码中的以下行

display = (Button) findViewById(R.id.tvDisplay);

display = (TextView) findViewById(R.id.tvDisplay);

您无法将TextView强制转换为按钮

答案 3 :(得分:0)

更改

display = (Button) findViewById(R.id.tvDisplay);

display = (TextView) findViewById(R.id.tvDisplay);

答案 4 :(得分:0)

这是一个非常简单的错误,它发生了。请将您的IDE和ADT更新为最新版本,因为新的ADT会在编码时捕获这些错误。这将节省大量的学习时间。