简体   繁体   English

ClassCastException:无法将LinearLayout强制转换为(java类)

[英]ClassCastException: LinearLayout cannot be cast to (java class)

Having an issue with the following error after trying to add a slideout menu to an activity: 尝试向活动添加滑出菜单后出现以下错误问题:

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

The specific section of code it is finding fault with is this: 它发现错误的代码的特定部分是这样的:

public class MainActivity extends Activity implements View.OnClickListener {

SlideOutMenu root;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.root = (SlideOutMenu) this.getLayoutInflater().inflate(R.layout.activity_main, null);
    this.setContentView(root);
}

Here is the relevant section of my xml file: 这是我的xml文件的相关部分:

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingLeft="0dp"
 android:paddingRight="0dp"
 android:id="@+id/mainActivityLayout"
 android:paddingTop="0dp"
 android:paddingBottom="0dp"
 tools:context=".MainActivity"
 android:background="#ffffff"
 android:orientation="vertical">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainActivitySlideout"
    android:background="#2a80b9"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tempBtn1"
        android:onClick="toggleMenu"
        android:text="Button 1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="toggleMenu"
        android:id="@+id/tempBtn2"
        android:text="Button 2"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tempBtn3"
        android:onClick="toggleMenu"
        android:text="Button 3"/>
</LinearLayout>

And this is my SlideOutMenu class, if required: 这是我的SlideOutMenu类,如果需要的话:

package rule02.touchpool;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class SlideOutMenu extends LinearLayout {
    private View menu;
    private View content;

    protected static final int menuMargin = 150;

    public enum MenuState {
        CLOSED, OPEN
    };

    protected int currentContentOffset = 0;
    protected MenuState menuCurrentState = MenuState.CLOSED;

    public SlideOutMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SlideOutMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SlideOutMenu(Context context) {
        super(context);
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        this.menu = this.getChildAt(0);
        this.content = this.getChildAt(1);
        this.menu.setVisibility(View.GONE);
    }

    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        if (changed) {
            this.calculateChildDimensions();
        }
        this.menu.layout(left, top, right - menuMargin, bottom);
        this.content.layout(left + this.currentContentOffset, top, right
                + this.currentContentOffset, bottom);
    }

    public void toggleMenu() {
        switch (this.menuCurrentState) {
        case CLOSED:
            this.menu.setVisibility(View.VISIBLE);
            this.currentContentOffset = this.getMenuWidth();
            this.content.offsetLeftAndRight(currentContentOffset);
            this.menuCurrentState = MenuState.OPEN;
            break;
        case OPEN:
            this.content.offsetLeftAndRight(-currentContentOffset);
            this.currentContentOffset = 0;
            this.menuCurrentState = MenuState.CLOSED;
            this.menu.setVisibility(View.GONE);
            break;
        }
        this.invalidate();
    }

    private int getMenuWidth() {
        return this.menu.getLayoutParams().width;
    }

    private void calculateChildDimensions() {
        this.content.getLayoutParams().height = this.getHeight();
        this.content.getLayoutParams().width = this.getWidth();
        this.menu.getLayoutParams().width = this.getWidth() - menuMargin;
        this.menu.getLayoutParams().height = this.getHeight();
    }
}

If anyone can help then I will be so very happy!! 如果有人可以帮助我,我将非常高兴! :) :)

Edit: 编辑:

Is it something to do with declaring SlideOutMenu as the root view in the xml document? 与在XML文档中将SlideOutMenu声明为根视图有关系吗? If so, I am not exactly sure how to do this and cannot find any info on it. 如果是这样,我不确定如何执行操作,也找不到任何相关信息。

indirect connected post & its indirect explanation , now follows the following code 间接连接的帖子 及其间接说明 ,现在遵循以下代码

SlideOutMenu root;
View container;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
container = getLayoutInflater().inflate(R.layout.activity_main, null);
root = (SlideOutMenu) container.findViewById(R.id.mainActivitySlideout);
 this.setContentView(container);
//the following codes can follow

your xml 您的xml

//the important part
<rule02.touchpool.SlideOutMenu 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainActivitySlideout"
android:background="#2a80b9"
android:orientation="vertical">

the rest is ok, remember to close it with </rule02.touchpool.SlideOutMenu> 其余的都还可以,请记住使用</rule02.touchpool.SlideOutMenu>将其关闭

I think root is your layout xml file. 我认为root是您的布局xml文件。 Therefore code should be, for fixing compile error: 因此,用于修复编译错误的代码应为:

this.root = (LinearLayout) this.getLayoutInflater().inflate(R.layout.activity_main, null);

Standard suggested code is: 标准建议代码为:

setContentView(R.layout.activity_main);

Note: I prefer the standard code. 注意:我更喜欢标准代码。

You can either use an XML or a class to create UI. 您可以使用XML或类来创建UI。 How are you using both and trying to convert one to another? 您如何同时使用两者并尝试将它们转换为另一种? It will never work. 它永远都行不通。 Either instantiate you layout java class and set it in content view or inflate your xml and set it. 实例化布局Java类并在内容视图中对其进行设置,或者对xml进行膨胀并对其进行设置。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(this.getLayoutInflater().inflate(R.layout.activity_main, null));
}

or you can do 或者你可以做

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout som = new SlideOutMenu(this);
    this.setContentView(som );
}

暂无
暂无

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

相关问题 java.lang.ClassCastException(无法将类转换为同一类) - java.lang.ClassCastException (Cannot cast class to same class) Java:ClassCastException-无法将java.lang.Class强制转换为 - Java: ClassCastException - java.lang.Class cannot be cast to 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 linearlayout.layoutParams强制转换为framelayout.layoutparams - Java.lang.classcastexception linearlayout.layoutParams cannot be cast to framelayout.layoutparams 错误:java.lang.ClassCastException LinearLayout$LayoutParams cannot be cast, when starting app - Error: java.lang.ClassCastException LinearLayout$LayoutParams cannot be cast ,when starting app 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:android.widget.LinearLayout无法转换为android.widget.Button - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM