gpt4 book ai didi

java - 类转换异常 : LinearLayout cannot be cast to (java class)

转载 作者:行者123 更新时间:2023-11-29 20:43:30 25 4
gpt4 key购买 nike

尝试向 Activity 添加滑出式菜单后出现以下错误:

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

它发现错误的代码的具体部分是这样的:

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);
}

这是我的 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>

如果需要,这是我的 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();
}
}

如果有人能提供帮助,我将非常高兴!! :)

编辑:

与在xml文档中声明SlideOutMenu为 Root View 有关吗?如果是这样,我不确定如何执行此操作,也找不到任何相关信息。

最佳答案

indirect connected post & its indirect explanation , 现在遵循以下代码

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

你的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">

剩下的就ok了,记得用</rule02.touchpool.SlideOutMenu>关闭

关于java - 类转换异常 : LinearLayout cannot be cast to (java class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com