简体   繁体   English

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

I have written a program for a navigation drawer which was working perfectly, but recently i have tried to put an ImageView for a profile picture and a TextView , after that it gives me a ClassCastException . 我已经编写了一个导航抽屉的程序,它完美地工作,但最近我试图为一个配置文件图片和一个TextView放一个ImageView ,之后它给了我一个ClassCastException

main_activity.xml : When I remove the LinearLayout inside that the ImageView and the TextView are, its working perfectly. main_activity.xml:当我删除ImageViewTextView内部的LinearLayout ,它的工作正常。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.example.shoppingcart.MainActivity" >

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/homeLogo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/home_logo" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#262626"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/profilePic"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="15dp"
            android:src="@drawable/shopping_cart" />

        <TextView
            android:id="@+id/textMailAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/profilePic"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="5dp"
            android:text="narahari.arjun@gmail.com"
            android:textColor="#ffffff" />

        <LinearLayout
            android:id="@+id/linear5"
            android:layout_width="match_parent"
            android:layout_height="7dp"
            android:layout_below="@+id/textMailAddress"
            android:layout_marginTop="7dp"
            android:orientation="vertical" >
        </LinearLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#7A7A7A" />

        <ListView
            android:id="@+id/drawerlist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/linear1"
            android:layout_gravity="start"
            android:background="#262626"
            android:choiceMode="singleChoice"
            android:divider="#7A7A7A"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

MainActivity.java : My code for navigation drawer with ImageView and TextView. MainActivity.java:我的带有ImageView和TextView的导航抽屉的代码。

public class MainActivity extends ActionBarActivity {
RelativeLayout relativeLayout;
private LinearLayout linearLayout;
String title = "Home";

ListView drawerList;
DrawerLayout drawer;
ActionBarDrawerToggle drawerListener;

private int openDrawerContentDescRes;

private int closeDrawerContentDescRes;
String[] Menus = { "Books", "Laptops", "Mobiles", "Fashion", "Sports", 
                   "Logout" };
int[] images = { R.drawable.books_logo, R.drawable.laptops_logo, 
        R.drawable.mobilelogo, R.drawable.fashion_logo,
        R.drawable.sports_logo, R.drawable.logout };

public static final String PREF_NAME = "Login";
String passedData, passedData1;
String data, data1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerList = (ListView) findViewById(R.id.drawerlist);
    drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
    passedData = getIntent().getStringExtra("emailID");
    passedData1 = getIntent().getStringExtra("pass");
    linearLayout = (LinearLayout) findViewById(R.id.linear1);
    SharedPreferences settings = getSharedPreferences(PREF_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    data = settings.getString("emailId", passedData);
    data1 = settings.getString("pass", passedData1);
    System.out.println("EmailID:" + data);
    System.out.println("Password:" + data1);
    System.out.println("EmailID:" + passedData);
    System.out.println("Password:" + passedData1);
    drawerListener = new ActionBarDrawerToggle(this, drawer, 
    R.drawable.navigation_menu, openDrawerContentDescRes,
            closeDrawerContentDescRes) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // TODO Auto-generated method stub
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
            super.onDrawerOpened(drawerView);
        }

    };

    drawer.setDrawerListener(drawerListener);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // getSupportActionBar().setTitle(" " + title);

    MyAdapter adapter = new MyAdapter(getBaseContext(), Menus, images);
    drawerList.setAdapter(adapter);
    drawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
            position, long id) {
            displayView(position);
            drawer.closeDrawer(drawerList);
        }

        private void selectItem(int position) {
            // TODO Auto-generated method stub
            drawerList.setItemChecked(position, true);
            setTitle(Menus[position]);

        }

        public void setTitle(String title) {
            getSupportActionBar().setTitle(title);

        }
    });

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    drawerListener.syncState();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if (drawerListener.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    drawerListener.onConfigurationChanged(newConfig);
}

private void displayView(int position) {
    // update the main content by replacing fragments
    android.app.Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new PhotosFragment();
        break;
    case 2:
        Intent intent = new Intent(MainActivity.this, MobileActivity.class);
        intent.putExtra("mobiles", "Mobiles");
        startActivity(intent);
        break;
    case 3:
        fragment = new CommunityFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        logout();
        break;
    default:
        break;
    }

    if (fragment != null) {
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frameLayout, 
        fragment).commit();

        // update selected item and title, then close the drawer
        drawerList.setItemChecked(position, true);
        drawerList.setSelection(position);
        setTitle(Menus[position]);
        drawer.closeDrawer(drawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

Logcat : Logcat:

05-13 22:51:51.914: E/AndroidRuntime(6666): FATAL EXCEPTION: main
05-13 22:51:51.914: E/AndroidRuntime(6666): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1126)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1331)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at com.example.shoppingcart.MainActivity$2.onItemClick(MainActivity.java:110)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.widget.AbsListView.performItemClick(AbsListView.java:1223)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.widget.ListView.performItemClick(ListView.java:4506)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2967)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.widget.AbsListView$1.run(AbsListView.java:3653)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.os.Handler.handleCallback(Handler.java:725)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.os.Looper.loop(Looper.java:158)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at android.app.ActivityThread.main(ActivityThread.java:5751)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at java.lang.reflect.Method.invoke(Method.java:511)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
05-13 22:51:51.914: E/AndroidRuntime(6666):     at dalvik.system.NativeStart.main(Native Method)

Should I change my XML code or java code? 我应该更改我的XML代码还是java代码?

Thank you! 谢谢!

You have an error here drawer.closeDrawer(drawerList); 你在这里有一个错误drawer.closeDrawer(drawerList); I think you should do this this way: drawer.closeDrawer(drawer) . 我认为你应该这样做: drawer.closeDrawer(drawer) You are using wrong layout, just change to DrawerLayout. 您使用了错误的布局,只需更改为DrawerLayout。

You are trying to close the list which isn't a "drawer". 您正试图关闭不是“抽屉”的列表。 Try using drawer.closeDrawers(); 尝试使用drawer.closeDrawers(); instead of drawer.closeDrawer(drawerList) . 而不是drawer.closeDrawer(drawerList)

drawerList.setOnItemClickListener(new OnItemClickListener() {

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int 
        position, long id) {
        displayView(position);
        drawer.closeDrawers();
    } 

    private void selectItem(int position) {
        // TODO Auto-generated method stub 
        drawerList.setItemChecked(position, true);
        setTitle(Menus[position]);

    } 

    public void setTitle(String title) {
        getSupportActionBar().setTitle(title);

    } 
}); 

暂无
暂无

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

相关问题 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams 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.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.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.LinearLayout$LayoutParams 不能转换为 android.support.constraint.ConstraintLayout$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams 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 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无法强制转换为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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM