[英]java.lang.ClassCastException (Cannot cast class to same class)
[英]ClassCastException: LinearLayout cannot be cast to (java class)
嘗試向活動添加滑出菜單后出現以下錯誤問題:
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聲明為根視圖有關系嗎? 如果是這樣,我不確定如何執行操作,也找不到任何相關信息。
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">
其余的都還可以,請記住使用</rule02.touchpool.SlideOutMenu>
將其關閉
我認為root
是您的布局xml文件。 因此,用於修復編譯錯誤的代碼應為:
this.root = (LinearLayout) this.getLayoutInflater().inflate(R.layout.activity_main, null);
標准建議代碼為:
setContentView(R.layout.activity_main);
注意:我更喜歡標准代碼。
您可以使用XML或類來創建UI。 您如何同時使用兩者並嘗試將它們轉換為另一種? 它永遠都行不通。 實例化布局Java類並在內容視圖中對其進行設置,或者對xml進行膨脹並對其進行設置。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(this.getLayoutInflater().inflate(R.layout.activity_main, null));
}
或者你可以做
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout som = new SlideOutMenu(this);
this.setContentView(som );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.