2013-02-10 63 views
0

我是Android新手。我想每一個我在大膽的代碼添加時間但是建立這個小程序:Android.widget textView到android.widget.button

d = (Button) findViewById (R.id.tvDis); 

應用程序崩潰,如果我刪除此行,一切工作正常。我在android 2.2 api 8版本和nexus的仿真器上運行。

package com.maximusstudios.numtowords; 

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


public class MainActivity extends Activity { 
/**Called when the activity is first created.*/ 
    int counter; 
    Button add, sub; 
    TextView d; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     counter = 0; 
     add = (Button) findViewById (R.id.bAdd); 
     **d = (Button) findViewById (R.id.tvDis);** 
     sub = (Button) findViewById(R.id.bSub); 
     add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter++; 

      } 
     }); 
sub.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter--; 
      } 
     }); 

    } 

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

} 

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Your Total is 0" 
     android:textSize="20dp" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:id="@+id/tvDis" 

     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add one" 
     android:layout_gravity="center" 
     android:textSize="20dp" 
     android:id="@+id/bAdd" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add one" 
     android:layout_gravity="center" 
     android:textSize="20dp" 
     android:id="@+id/bSub" 
     /> 

</LinearLayout> 

錯誤列表:

02-10 18:00:21.452: D/AndroidRuntime(910): Shutting down VM 
02-10 18:00:21.452: W/dalvikvm(910): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
02-10 18:00:21.472: E/AndroidRuntime(910): FATAL EXCEPTION: main 
02-10 18:00:21.472: E/AndroidRuntime(910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maximusstudios.numtowords/com.maximusstudios.numtowords.MainActivity}: java.lang.ClassCastException: android.widget.TextView 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.os.Looper.loop(Looper.java:123) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-10 18:00:21.472: E/AndroidRuntime(910): at java.lang.reflect.Method.invokeNative(Native Method) 
02-10 18:00:21.472: E/AndroidRuntime(910): at java.lang.reflect.Method.invoke(Method.java:521) 
02-10 18:00:21.472: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-10 18:00:21.472: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-10 18:00:21.472: E/AndroidRuntime(910): at dalvik.system.NativeStart.main(Native Method) 
02-10 18:00:21.472: E/AndroidRuntime(910): Caused by: java.lang.ClassCastException: android.widget.TextView 
02-10 18:00:21.472: E/AndroidRuntime(910): at com.maximusstudios.numtowords.MainActivity.onCreate(MainActivity.java:23) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-10 18:00:21.472: E/AndroidRuntime(910): ... 11 more 
+2

我只注意到你的問題標題。您似乎知道您正在將TextView投射到Button。你想做什麼? – Simon 2013-02-10 22:21:28

+0

哦,如果一切正常,當你把線路取出時,爲什麼它在那裏? – Simon 2013-02-10 22:35:42

+0

@Simon我認爲這是因爲OP會在解決這個問題後的某個時候使用它。 – 2013-02-10 22:49:04

回答

4

更改爲

d = (TextView) findViewById (R.id.tvDis); 

您的投放是錯誤的。使用您的代碼,當您執行(Button)時,您試圖將由findViewById()返回的TextView轉換爲Button,由於它們是不兼容的類型,因此不起作用。

+1

謝謝sooo多我是一個傻瓜:S – Kayoti 2013-02-11 01:42:22

+0

@Kayoti沒有問題,小錯誤發生在我們所有人身上!您應該將*答案中的一個*標記爲[通過點擊被挖空的選中標記使其變爲綠色](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) 。 – 2013-02-11 02:10:52

3

您定義d爲TextView並嘗試將其轉換爲Button

應該

d = (TextView) findViewById (R.id.tvDis); 

你得到的錯誤是ClassCastException

+0

非常感謝你 – Kayoti 2013-02-11 01:58:02

相關問題