简体   繁体   English

java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.Button

[英]java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button

I do have a short Android app I am buiding using Eclipse ADT plugin that have two activities. 我确实使用Eclipse ADT插件构建了一个简短的Android应用程序,该应用程序有两个活动。 The main activity have a button that when clicked, calls the second screen that displays my name. 主要活动有一个按钮,单击该按钮会调用第二个显示我名字的屏幕。 However, my application stops working on running it. 但是,我的应用程序停止运行。 The error I get is: 我得到的错误是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.test / com.example.test.MainActivity}:java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.Button

How can I solve this error? 我该如何解决这个错误?

Here is my main activity code: 这是我的主要活动代码:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button batta=(Button)findViewById(R.id.btnICT3631);
    batta.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            //my code goes here
            startActivity(new Intent(MainActivity.this, ICT3631.class));
        }
    }); //end but1



}


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

}

This exception means that you are using button in your xml file while casting it to the linear layout in your class file. 此异常意味着您正在将xml文件中的按钮投射到类文件中的线性布局时使用。 Please post your code such that I can give you a better explanation. 请发布您的代码,以便我给您更好的解释。

In case you experience the error 万一您遇到错误

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button

Even if you try change layout the problem will still persist. 即使您尝试更改布局,问题仍然会存在。 As I got this answer helpful. 当我得到这个答案很有帮助。 I thought I could say it simple for other guys to understand. 我以为我可以说其他人很容易理解。 All you need do is to make sure that in your main activity layout xml file you dont have the view id that the error state you trying to cast defined twice. 您需要做的就是确保在主活动布局xml文件中没有要尝试两次定义的错误状态的视图ID。 In my case, I have the button view id defined both in the layout level and on the view level too. 就我而言,我在布局级别和视图级别都定义了按钮视图ID。 So deleting the view id declaration in the layot fixed the issue and only have defined in that specific view - in my case the button view id. 因此,删除布局中的视图ID声明可以解决此问题,并且仅在该特定视图中进行了定义-在我的情况下是按钮视图ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.ListView - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView java.lang.ClassCastException:android.widget.LinearLayout 无法转换为 android.widget - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget java.lang.ClassCastException:androidx.constraintlayout.widget.ConstraintLayout 无法转换为 android.widget.LinearLayout - java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton 不能转换为 android.widget.Button - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout - ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout Android java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams - Android java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM