ClassCastException: java.lang.ClassCastException: android.widget.LinearLayout $ LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout

I wrote a program for the nav-drawer that works fine, but lately I tried to put ImageView

for a profile picture and TextView

after which it gives me ClassCastException

.

main_activity.xml: When I remove LinearLayout

inside ImageView

and TextView

, it works fine.

<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 the navbox with ImageView and 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:

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 or java code?

Thank!

+3


source to share


2 answers


You have an error here drawer.closeDrawer(drawerList);

I think you should do this as follows: drawer.closeDrawer(drawer)

. You are using the wrong layout, just change to DrawerLayout.



+1


source


You are trying to close a list that is not a box. Try using instead . drawer.closeDrawers();

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

    } 
}); 

      

+1


source







All Articles