Java Android.widget文本查看到Android.widget.button

Java Android.widget文本查看到Android.widget.button,java,android,xml,android-layout,android-widget,Java,Android,Xml,Android Layout,Android Widget,我是Android新手。我正在尝试构建这个小小的应用程序,但每次我添加粗体代码时: d = (Button) findViewById (R.id.tvDis); 应用程序崩溃,如果我删除这一行,一切正常。我在安卓2.2API8版本和NexusS模拟器上运行这个 package com.maximusstudios.numtowords; import android.os.Bundle; import android.app.Activity; import android.view.Me

我是Android新手。我正在尝试构建这个小小的应用程序,但每次我添加粗体代码时:

d = (Button) findViewById (R.id.tvDis);
应用程序崩溃,如果我删除这一行,一切正常。我在安卓2.2API8版本和NexusS模拟器上运行这个

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>
改为

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

你的演员选错了。在代码中,当您执行
(按钮)
操作时,您试图将
findViewById()
返回的
文本视图
转换为
按钮
,该按钮不工作,因为它们是不兼容的类型。

您将d定义为
文本视图
,并尝试将其转换为
按钮

应该是

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

您遇到的错误是一个
ClassCastException

我刚刚注意到您的问题标题。您似乎知道您正在将TextView转换为按钮。你想做什么?哦,如果你把电话线拔出来时一切正常,为什么会在那里?@Simon我想这是因为OP在解决这个问题后的某个时候会使用它。@Kayoti没问题,小错误会发生在我们所有人身上!你应该把其中一个答案标记为。
d = (TextView) findViewById (R.id.tvDis);