引起的:. lang。android.widget ClassCastException。不能将RelativeLayout写入com.slidingmenu.lib.SlidingMenu .slidingmenu

时间:2023-01-20 23:31:51

I've just read all questions about this problem at * and some forums, but still have no solution. I tried to remove R.java, clear the cache, edit .xml but nothing helps.

我刚刚在*和一些论坛上看到了关于这个问题的所有问题,但是仍然没有解决办法。我试着去除了R。java,清除缓存,编辑.xml,但没有任何帮助。

Error text:

错误的文本:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.easyten.app/com.easyten.app.EasyTenActivity}: 
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.slidingmenu.lib.SlidingMenu ...
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.slidingmenu.lib.SlidingMenu
        at com.slidingmenu.lib.app.SlidingActivityHelper.onCreate(SlidingActivityHelper.java:32)

This is the code:

slidingmenumain.xml

这是代码:slidingmenumain.xml

<?xml version="1.0" encoding="utf-8"?>
<com.slidingmenu.lib.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slidingmenumain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


SlidingActivityHelper.java

SlidingActivityHelper.java

package com.slidingmenu.lib.app;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.slidingmenu.lib.R;
import com.slidingmenu.lib.SlidingMenu;

public class SlidingActivityHelper {

    private Activity mActivity;

    private SlidingMenu mSlidingMenu;
    private View mViewAbove;
    private View mViewBehind;
    private boolean mBroadcasting = false;

    private boolean mOnPostCreateCalled = false;
    private boolean mEnableSlide = true;

    public SlidingActivityHelper(Activity activity) {
        mActivity = activity;
    }

    public void onCreate(Bundle savedInstanceState) {
        mSlidingMenu = (SlidingMenu) LayoutInflater.from(mActivity).inflate(R.layout.slidingmenumain, null);
    }

    ...other code
}


32 line of SlidingActivityHelper.java is mSlidingMenu = (SlidingMenu) LayoutInflater.from(mActivity).inflate(R.layout.slidingmenumain, null);

32行SlidingActivityHelper。java是mSlidingMenu = (SlidingMenu) layoutinflaterm .from(mActivity). inflation (R.layout.slidingmenumain, null);


SlidingMenu.java

SlidingMenu.java

package com.slidingmenu.lib;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.slidingmenu.lib.CustomViewAbove.OnPageChangeListener;

public class SlidingMenu extends RelativeLayout {

    public static final int TOUCHMODE_MARGIN = 0;
    public static final int TOUCHMODE_FULLSCREEN = 1;

    private CustomViewAbove mViewAbove;
    private CustomViewBehind mViewBehind;
    private OnOpenListener mOpenListener;
    private OnCloseListener mCloseListener;

    //private boolean mSlidingEnabled;

    public static void attachSlidingMenu(Activity activity, SlidingMenu sm, boolean slidingTitle) {

        if (sm.getParent() != null)
            throw new IllegalStateException("SlidingMenu cannot be attached to another view when" +
                    " calling the static method attachSlidingMenu");

        if (slidingTitle) {
            // get the window background
            TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
            int background = a.getResourceId(0, 0);
            // move everything into the SlidingMenu
            ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
            ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
            decor.removeAllViews();
            // save ActionBar themes that have transparent assets
            decorChild.setBackgroundResource(background);
            sm.setContent(decorChild);
            decor.addView(sm);
        } else {
            // take the above view out of
            ViewGroup content = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
            View above = content.getChildAt(0);
            content.removeAllViews();
            sm.setContent(above);
            content.addView(sm, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }
    }

    public interface OnOpenListener {
        public void onOpen();
    }

    public interface OnOpenedListener {
        public void onOpened();
    }

    public interface OnCloseListener {
        public void onClose();
    }

    public interface OnClosedListener {
        public void onClosed();
    }

    public interface CanvasTransformer {
        public void transformCanvas(Canvas canvas, float percentOpen);
    }

    public SlidingMenu(Context context) {
        this(context, null);
    }

    public SlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

        LayoutParams behindParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mViewBehind = new CustomViewBehind(context);
        addView(mViewBehind, behindParams);
        LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mViewAbove = new CustomViewAbove(context);
        addView(mViewAbove, aboveParams);
        // register the CustomViewBehind2 with the CustomViewAbove
        mViewAbove.setCustomViewBehind(mViewBehind);
        mViewBehind.setCustomViewAbove(mViewAbove);
        mViewAbove.setOnPageChangeListener(new OnPageChangeListener() {
            public static final int POSITION_OPEN = 0;
            public static final int POSITION_CLOSE = 1;

            public void onPageScrolled(int position, float positionOffset,
                    int positionOffsetPixels) { }

            public void onPageSelected(int position) {
                if (position == POSITION_OPEN && mOpenListener != null) {
                    mOpenListener.onOpen();
                } else if (position == POSITION_CLOSE && mCloseListener != null) {
                    mCloseListener.onClose();
                }
            }
        });

        // now style everything!
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu);
        // set the above and behind views if defined in xml
        int viewAbove = ta.getResourceId(R.styleable.SlidingMenu_viewAbove, -1);
        if (viewAbove != -1)
            setContent(viewAbove);
        int viewBehind = ta.getResourceId(R.styleable.SlidingMenu_viewBehind, -1);
        if (viewBehind != -1)
            setMenu(viewBehind);
        int touchModeAbove = ta.getInt(R.styleable.SlidingMenu_aboveTouchMode, TOUCHMODE_MARGIN);
        setTouchModeAbove(touchModeAbove);
        int touchModeBehind = ta.getInt(R.styleable.SlidingMenu_behindTouchMode, TOUCHMODE_MARGIN);
        setTouchModeBehind(touchModeBehind);

        int offsetBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindOffset, -1);
        int widthBehind = (int) ta.getDimension(R.styleable.SlidingMenu_behindWidth, -1);
        if (offsetBehind != -1 && widthBehind != -1)
            throw new IllegalStateException("Cannot set both behindOffset and behindWidth for a SlidingMenu");
        else if (offsetBehind != -1)
            setBehindOffset(offsetBehind);
        else if (widthBehind != -1)
            setBehindWidth(widthBehind);
        else
            setBehindOffset(0);
        float scrollOffsetBehind = ta.getFloat(R.styleable.SlidingMenu_behindScrollScale, 0.33f);
        setBehindScrollScale(scrollOffsetBehind);
        int shadowRes = ta.getResourceId(R.styleable.SlidingMenu_shadowDrawable, -1);
        if (shadowRes != -1) {
            setShadowDrawable(shadowRes);
        }
        int shadowWidth = (int) ta.getDimension(R.styleable.SlidingMenu_shadowWidth, 0);
        setShadowWidth(shadowWidth);
        boolean fadeEnabled = ta.getBoolean(R.styleable.SlidingMenu_behindFadeEnabled, true);
        setFadeEnabled(fadeEnabled);
        float fadeDeg = ta.getFloat(R.styleable.SlidingMenu_behindFadeDegree, 0.66f);
        setFadeDegree(fadeDeg);
        boolean selectorEnabled = ta.getBoolean(R.styleable.SlidingMenu_selectorEnabled, false);
        setSelectorEnabled(selectorEnabled);
        int selectorRes = ta.getResourceId(R.styleable.SlidingMenu_selectorDrawable, -1);
        if (selectorRes != -1)
            setSelectorDrawable(selectorRes);
    }

    public void setContent(int res) {
        setContent(LayoutInflater.from(getContext()).inflate(res, null));
    }

    public void setContent(View v) {
        mViewAbove.setContent(v);
        mViewAbove.invalidate();
        showAbove(true);
    }

    public void setMenu(int res) {
        setMenu(LayoutInflater.from(getContext()).inflate(res, null));
    }

    public void setMenu(View v) {
        mViewBehind.setMenu(v);
        mViewBehind.invalidate();
    }

    public void setSlidingEnabled(boolean b) {
        mViewAbove.setSlidingEnabled(b);
    }

    public boolean isSlidingEnabled() {
        return mViewAbove.isSlidingEnabled();
    }

    /**
     * 
     * @param b Whether or not the SlidingMenu is in a static mode 
     * (i.e. nothing is moving and everything is showing)
     */
    public void setStatic(boolean b) {
        if (b) {
            setSlidingEnabled(false);
            mViewAbove.setCustomViewBehind(null);
            mViewAbove.setCurrentItem(1);
            mViewBehind.setCurrentItem(0);  
        } else {
            mViewAbove.setCurrentItem(1);
            mViewBehind.setCurrentItem(1);
            mViewAbove.setCustomViewBehind(mViewBehind);
            setSlidingEnabled(true);
        }
    }

    /**
     * Shows the behind view
     */
    public void showBehind(boolean b) {
        mViewAbove.setCurrentItem(0, b);
    }

    public void showBehind() {
        mViewAbove.setCurrentItem(0);
    }

    /**
     * Shows the above view
     */
    public void showAbove(boolean b) {
        mViewAbove.setCurrentItem(1, b);
    }

    public void showAbove() {
        mViewAbove.setCurrentItem(1);
    }

    /**
     * 
     * @return Whether or not the behind view is showing
     */
    public boolean isBehindShowing() {
        return mViewAbove.getCurrentItem() == 0;
    }

    /**
     * 
     * @return The margin on the right of the screen that the behind view scrolls to
     */
    public int getBehindOffset() {
        return ((RelativeLayout.LayoutParams)mViewBehind.getLayoutParams()).rightMargin;
    }

    /**
     * 
     * @param i The margin on the right of the screen that the behind view scrolls to
     */
    public void setBehindOffset(int i) {
        RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)mViewBehind.getLayoutParams());
        int bottom = params.bottomMargin;
        int top = params.topMargin;
        int left = params.leftMargin;
        params.setMargins(left, top, i, bottom);
    }

    /**
     * 
     * @param i The width the Sliding Menu will open to in pixels
     */
    @SuppressWarnings("deprecation")
    public void setBehindWidth(int i) {
        int width;
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        try {
            Class<?> cls = Display.class;
            Class<?>[] parameterTypes = {Point.class};
            Point parameter = new Point();
            Method method = cls.getMethod("getSize", parameterTypes);
            method.invoke(display, parameter);
            width = parameter.x;
        } catch (Exception e) {
            width = display.getWidth();
        }
        setBehindOffset(width-i);
    }

    /**
     * 
     * @param res A resource ID which points to the width the Sliding Menu will open to 
     */
    public void setBehindWidthRes(int res) {
        int i = (int) getContext().getResources().getDimension(res);
        setBehindWidth(i);
    }

    /**
     * 
     * @param res The dimension resource to be set as the behind offset
     */
    public void setBehindOffsetRes(int res) {
        int i = (int) getContext().getResources().getDimension(res);
        setBehindOffset(i);
    }

    /**
     * 
     * @return The scale of the parallax scroll
     */
    public float getBehindScrollScale() {
        return mViewAbove.getScrollScale();
    }

    /**
     * 
     * @param f The scale of the parallax scroll (i.e. 1.0f scrolls 1 pixel for every
     * 1 pixel that the above view scrolls and 0.0f scrolls 0 pixels)
     */
    public void setBehindScrollScale(float f) {
        mViewAbove.setScrollScale(f);
    }

    public void setBehindCanvasTransformer(CanvasTransformer t) {
        mViewBehind.setCanvasTransformer(t);
    }

    public int getTouchModeAbove() {
        return mViewAbove.getTouchMode();
    }

    public void setTouchModeAbove(int i) {
        if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN) {
            throw new IllegalStateException("TouchMode must be set to either" +
                    "TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN.");
        }
        mViewAbove.setTouchMode(i);
    }

    public int getTouchModeBehind() {
        return mViewBehind.getTouchMode();
    }

    public void setTouchModeBehind(int i) {
        if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN) {
            throw new IllegalStateException("TouchMode must be set to either" +
                    "TOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN.");
        }
        mViewBehind.setTouchMode(i);
    }

    public void setShadowDrawable(int resId) {
        mViewAbove.setShadowDrawable(resId);
    }

    public void setShadowWidthRes(int resId) {
        setShadowWidth((int)getResources().getDimension(resId));
    }

    public void setShadowWidth(int pixels) {
        mViewAbove.setShadowWidth(pixels);
    }

    public void setFadeEnabled(boolean b) {
        mViewAbove.setBehindFadeEnabled(b);
    }

    public void setFadeDegree(float f) {
        mViewAbove.setBehindFadeDegree(f);
    }

    public void setSelectorEnabled(boolean b) {
        mViewAbove.setSelectorEnabled(true);
    }

    public void setSelectedView(View v) {
        mViewAbove.setSelectedView(v);
    }

    public void setSelectorDrawable(int res) {
        mViewAbove.setSelectorDrawable(BitmapFactory.decodeResource(getResources(), res));
    }

    public void setSelectorDrawable(Bitmap b) {
        mViewAbove.setSelectorDrawable(b);
    }

    public void setOnOpenListener(OnOpenListener listener) {
        //mViewAbove.setOnOpenListener(listener);
        mOpenListener = listener;
    }

    public void setOnCloseListener(OnCloseListener listener) {
        //mViewAbove.setOnCloseListener(listener);
        mCloseListener = listener;
    }

    public void setOnOpenedListener(OnOpenedListener listener) {
        mViewAbove.setOnOpenedListener(listener);
    }

    public void setOnClosedListener(OnClosedListener listener) {
        mViewAbove.setOnClosedListener(listener);
    }

    private static class SavedState extends BaseSavedState {
        boolean mBehindShowing;

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeBooleanArray(new boolean[]{mBehindShowing});
        }

        /*
        public static final Parcelable.Creator<SavedState> CREATOR
        = ParcelableCompat.newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {

            public SavedState createFromParcel(Parcel in, ClassLoader loader) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        });

        SavedState(Parcel in) {
            super(in);
            boolean[] showing = new boolean[1];
            in.readBooleanArray(showing);
            mBehindShowing = showing[0];
        }
        */
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.mBehindShowing = isBehindShowing();
        return ss;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState ss = (SavedState)state;
        super.onRestoreInstanceState(ss.getSuperState());

        if (ss.mBehindShowing) {
            showBehind(true);
        } else {
            showAbove(true);
        }
    }

    @Override
    protected boolean fitSystemWindows(Rect insets) {

        int leftPadding = getPaddingLeft() + insets.left;
        int rightPadding = getPaddingRight() + insets.right;
        int topPadding = insets.top;
        int bottomPadding = insets.bottom;
        this.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);

        return super.fitSystemWindows(insets);
    }

}

What's wrong? How can I fix it? I'm using IntelliJ IDEA 12 CE. Thanks a lot for any help!

怎么了?我怎样才能修好它?我用的是IntelliJ IDEA 12 CE。非常感谢您的帮助!

3 个解决方案

#1


1  

This can be resolved by recreating the R.java file.

这可以通过重新创建R来解决。java文件。

1. Re-build OR

1。重建或

2. Change the 'android:id' in XML file to a new id and re-build. Make sure you manually change all references to this id.

2。将XML文件中的“android:id”更改为新的id并重新构建。确保您手动更改了对该id的所有引用。

#2


0  

Sometimes this does´t work even recreating the R.java file, cleaning and rebuilding the project.

有时这´t工作甚至重建R。java文件,清理和重建项目。

for me it works until i delete \gen and \bin folders, then automatically were recreated with the new relationship between resources and their id´s.

对我来说工作直到我删除\创和\ bin文件夹,然后自动重新创建了新的资源及其之间的关系id´s。

#3


-1  

I think the problem is just that the inflater cast your item to RelativeLayout and you can't cast a relativeLayout in your item (Maybe this post is more precise : ClassCastException). Perhaps you have to give up and retrieve your object using your id with findViewById.

我认为问题在于打气筒将你的项目投给了相对论布局,而你不能在你的项目中投一个相对论布局(也许这篇文章更精确:ClassCastException)。也许您必须放弃使用findViewById使用您的id来检索对象。

#1


1  

This can be resolved by recreating the R.java file.

这可以通过重新创建R来解决。java文件。

1. Re-build OR

1。重建或

2. Change the 'android:id' in XML file to a new id and re-build. Make sure you manually change all references to this id.

2。将XML文件中的“android:id”更改为新的id并重新构建。确保您手动更改了对该id的所有引用。

#2


0  

Sometimes this does´t work even recreating the R.java file, cleaning and rebuilding the project.

有时这´t工作甚至重建R。java文件,清理和重建项目。

for me it works until i delete \gen and \bin folders, then automatically were recreated with the new relationship between resources and their id´s.

对我来说工作直到我删除\创和\ bin文件夹,然后自动重新创建了新的资源及其之间的关系id´s。

#3


-1  

I think the problem is just that the inflater cast your item to RelativeLayout and you can't cast a relativeLayout in your item (Maybe this post is more precise : ClassCastException). Perhaps you have to give up and retrieve your object using your id with findViewById.

我认为问题在于打气筒将你的项目投给了相对论布局,而你不能在你的项目中投一个相对论布局(也许这篇文章更精确:ClassCastException)。也许您必须放弃使用findViewById使用您的id来检索对象。