Android.widget textView 到 android.widget.button
Posted
技术标签:
【中文标题】Android.widget textView 到 android.widget.button【英文标题】:Android.widget textView to android.widget.button 【发布时间】:2013-01-26 00:42:27 【问题描述】:我是 android 新手。我正在尝试构建这个小应用程序,但是每次我以粗体添加代码时:
d = (Button) findViewById (R.id.tvDis);
应用程序崩溃,如果我删除此行,一切正常。我在 android 2.2 api 8 版本和 nexus s 模拟器上运行它。
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_
android:layout_
tools:context=".MainActivity" >
<TextView
android:layout_
android:layout_
android:text="Your Total is 0"
android:textSize="20dp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDis"
/>
<Button
android:layout_
android:layout_
android:text="Add one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/bAdd"
/>
<Button
android:layout_
android:layout_
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 ComponentInfocom.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
【问题讨论】:
我刚刚注意到您的问题标题。您似乎知道您正在将 TextView 转换为 Button。你想做什么? 哦,如果你把线拿出来时一切正常,那为什么会在那里? @Simon 我认为这是因为 OP 在解决此问题后会在某个时候使用它。 【参考方案1】:改成
d = (TextView) findViewById (R.id.tvDis);
你的选角有误。使用您的代码,当您执行(Button)
时,您正试图将findViewById()
返回的TextView
转换为Button
,由于它们是不兼容的类型,这不起作用。
【讨论】:
@Kayoti 没问题,小错误发生在我们所有人身上!您应该将一个的答案标记为accepted by clicking the hollowed out checkmark so it turns green。【参考方案2】:您将 d 定义为 TextView
并尝试将其转换为 Button
应该是
d = (TextView) findViewById (R.id.tvDis);
你得到的错误是ClassCastException
【讨论】:
以上是关于Android.widget textView 到 android.widget.button的主要内容,如果未能解决你的问题,请参考以下文章
android.widget.LinearLayout 无法转换为 android.widget.TextView
android.widget.TextView.setTextAlignment():java.lang.NoSuchMethodError
Android 错误:android.widget.TextView.<init>
带有子项的 Android ListView 导致无法将 TwoLineListItem 强制转换为 android.widget.TextView
异步任务尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'[重复]