Error: can not cast

0

I am implementing the Navigation Drawer in my application. Only I get an error when I click on any of my items.

Here the code:

public class MainActivityMenu extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
ProgressDialog dialog;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mTitle = mDrawerTitle = getTitle();


    /*DrawerLayout.LayoutParams params = img.getLayoutParams();
    params.width = DrawerLayout.LayoutParams.MATCH_PARENT;
    params.height = 100;
    img.setLayoutParams(params);*/
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(MainActivityMenu.this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
    mDrawerToggle.syncState();


    SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
    TextView Usuario = (TextView)findViewById(R.id.userName);
    Usuario.setText( "Usuario: " + settings.getString("ONControlUsuarioLogin",""));

}

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String evento = String.valueOf(((TextView)view.findViewById(R.id.txtevento)).getText());
      selectItem(position);

    }

}

private void selectItem(int position) {

    Fragment fragment = null;
  /*  Bundle args = new Bundle();
    args.putInt(ClientesFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);*/
    //android.app.Fragment fragment1 = null;


    switch (position) {
        case 0:
            Toast.makeText(getApplicationContext(),"Prueba1", Toast.LENGTH_LONG).show();
            //fragment = new ConnectFragment();
            break;
        case 1:
            fragment = new ClientesFragment();
            Bundle args = new Bundle();
            args.putSerializable("O_DetalleMenu", (DetalleMenu)((TextView)findViewById(R.id.lismenu)).getTag());
            fragment.setArguments(args);

            // fragment = new ClientesFragment();
            break;
        case 2:
            //fragment = new TableFragment();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.mainContent, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        //setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

Layout Code Attachment

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

</FrameLayout>


<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
<!-- The main content view -->
<RelativeLayout
    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
</FrameLayout>



<!-- The navigation drawer -->
<RelativeLayout
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:id="@+id/drawerPane"
    android:layout_gravity="start">



<!-- Profile Box -->

<RelativeLayout
    android:id="@+id/profileBox"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="@color/material_blue_grey_800"
    android:padding="8dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/icon_oncontrol"
        android:layout_marginTop="15dp" />

    <TextView
        android:id="@+id/userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Usuario"
        android:textColor="#fff"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_below="@+id/avatar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="7dp" />


</RelativeLayout>


<ListView

    android:id="@+id/left_drawer"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_below="@+id/profileBox"
    android:background="@color/ColorBackground1"/>

    <!--  <include
          android:menu="@menu/activity_main_drawer"
          />
-->
</RelativeLayout>

And the error

07-25 13:09:39.312 15344-15344/mx.com.oncontrol.oncontrol E/AndroidRuntime: FATAL EXCEPTION: main
Process: mx.com.oncontrol.oncontrol, PID: 15344
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1415)
    at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1630)
    at mx.com.oncontrol.oncontrol.MainActivityMenu.selectItem(MainActivityMenu.java:125)
    at mx.com.oncontrol.oncontrol.MainActivityMenu.access$000(MainActivityMenu.java:29)
    at mx.com.oncontrol.oncontrol.MainActivityMenu$DrawerItemClickListener.onItemClick(MainActivityMenu.java:72)
    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
    at android.widget.AbsListView$3.run(AbsListView.java:3645)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)

    
asked by Hugo Rodriguez 25.07.2017 в 20:14
source

1 answer

1

android.widget.RelativeLayout$LayoutParams and android.support.v4.widget.DrawerLayout$LayoutParams are 2 different classes even if they have the same name. It only imports the class that you are going to use when you are going to assign the layout parameters in the DrawerLayout, in this case android.support.v4.widget.DrawerLayout$LayoutParams

    
answered by 25.07.2017 в 20:32