Textview导致java.lang.ClassCastException:android.widget.Textview
我有三个文本视图,并对它们应用了clickevent,但当我单击其中任何一个时,它们会在应用程序中导致Forceclose错误。我还尝试过更改ids textviews,然后清理项目并运行项目,但仍然没有删除错误 我的XML代码是 对于一个文本视图Textview导致java.lang.ClassCastException:android.widget.Textview,android,textview,Android,Textview,我有三个文本视图,并对它们应用了clickevent,但当我单击其中任何一个时,它们会在应用程序中导致Forceclose错误。我还尝试过更改ids textviews,然后清理项目并运行项目,但仍然没有删除错误 我的XML代码是 对于一个文本视图 <LinearLayout android:id="@+id/LL_fb" android:layout_width="180px" android:layout_height="27px"
<LinearLayout
android:id="@+id/LL_fb"
android:layout_width="180px"
android:layout_height="27px"
android:layout_above="@+id/txt_msg_regs"
android:layout_alignLeft="@+id/LL_signup"
android:layout_marginBottom="25dp"
android:background="@drawable/facebook" >
<TextView
android:id="@+id/btn_txt_fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect with facebook"
android:layout_gravity="center_vertical"
android:layout_marginLeft="23dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:textColor="#FFFFFF" />
</LinearLayout>
请告诉我这有什么问题。如果需要logcat,请向我索取
findViewById(R.id.btn_txt_login);
这真的是一个文本视图还是你的布局中也有一个按钮?我认为您试图将一个按钮强制转换到文本视图错误的资源ID
img_login = (TextView)findViewById(R.id.btn_txt_login);
根据您的XML应该是
img_login = (TextView)findViewById(R.id.txt_login);
等等请发布完整的XMl,以便我们可以在其中查看btn_txt_登录
它看起来是一个按钮,因为如果此“btn_txt_登录”id不是xml格式,则“findViewById(R.id.btn_txt_登录)”将返回null。用此更改您的代码
TextView img_login;
img_login = (TextView)findViewById(R.id.txt_login);
img_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId().equals(img_login.getId()))
{
Intent i_lin = new Intent(LockmeifyoucanActivity.this,lmiyc_login.class);
startActivity(i_lin);
}
}
});
要快速解决此错误,请在直线上设置一个断点:
View view = findViewById(R.id.btn_txt_login);
然后,查看本地选项卡上的哪个组件,查看
gen/R.java。
您在lmiyc_登录活动中的文本视图有问题。在lmiyc_登录对应的第46行中,可能:
TextView txtview;
txtview = (TextView)yourlayout.findViewById(R.id.txtviewid);
这些是42-48的行
setContentView(R.layout.lmyic_login_page);
txtBack = (TextView)findViewById(R.id.ImgViewTxtBack); txtBack.setTypeface(null,Typeface.BOLD);
iv_login = (ImageView)findViewById(R.id.Txtlogin);
iv_login.setOnClickListener(this);
iv_sign_up = (ImageView)findViewById(R.id.TxtSignup);
我认为问题在于iv_login=(ImageView)findviewbyd(R.id.Txtlogin);-德雷什·辛格1分钟前编辑
因为这是46行,应该是textview,并且您正在Imageview中输入它–Dheresh Singh刚刚编辑了,如果您有重复的资源Id,这将抛出ClasscastException。因此,确保在整个项目中没有重复的元素id。btn\U txt\U登录在哪里?请发布有btn_txt_登录的XMl。@SamirMangroliya您好,我错误地编辑了我的问题在我的XMl代码中是btn_txt_登录的,它是这样写的……发布日志我确信您在lmiyc_login.class活动中遇到了一些问题您尝试了(v.getId()=(R.id.btn_txt_登录))而不是v.getId()==img_login.getId()@SamirMangroliya logcat是btn_txt_登录应该是UI上的按钮,我们可以看到txt_登录是textview。我想,由于几乎相同的名字,您可能会这样做。如果查询已解决,请投票/接受任何与您的答案接近的答案,以完成此故事,并让所有人都清楚地了解…当我单击主页上的登录btn,然后单击此屏幕(lmiyc_登录)时,这几行代码是我的另一页代码应该是打开的,并且它还有一个登录btn,与主屏幕中的登录btn相同。如果您为lmyic_login_页面xml提供pastbin链接,那么您的问题将以排序方式解决time@all问题解决了。问题在另一页。在lmiyc_登录页面中将所有imageview重新声明为textview。哈哈哈,你写了btn_txt_登录n我写了这个R.id.txt_登录,你写了这个v.getId()==img_login.getId(),我写了这个v.getId().equals(img_login.getId()),你说它已经很好了:)
View view = findViewById(R.id.btn_txt_login);
TextView txtview;
txtview = (TextView)yourlayout.findViewById(R.id.txtviewid);
setContentView(R.layout.lmyic_login_page);
txtBack = (TextView)findViewById(R.id.ImgViewTxtBack); txtBack.setTypeface(null,Typeface.BOLD);
iv_login = (ImageView)findViewById(R.id.Txtlogin);
iv_login.setOnClickListener(this);
iv_sign_up = (ImageView)findViewById(R.id.TxtSignup);