android.widget.Framelayout$Layoutparams cannot be cast to android.support.v4.widget.Drawerlayout$Layoutparams

Asked

Viewed 90 times

2

I’m trying to close the Drawer when I click on an item, however I’m getting the error:

android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

Does anyone know what it can be?

My code:

main_activity.xml

<android.support.v7.widget.Toolbar
    android:id = "@+id/toolbarTelaOpcoes"
    android:layout_height = "?attr/actionBarSize"
    android:layout_width = "match_parent"
    android:minHeight = "?attr/actionBarSize"
    android:background = "?attr/colorPrimary"
    android:elevation = "4dp" />

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

    <FrameLayout
        android:id = "@+id/content_frame"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:orientation = "vertical">

        <ListView
            android:id = "@+id/left_drawer"
            android:layout_width = "240dp"
            android:layout_height = "match_parent"
            android:layout_gravity = "left"
            android:background="@color/white"
            android:choiceMode = "singleChoice"
            android:divider = "@color/black"
            android:dividerHeight = "0dp"/>
    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

Mainactivity

public class OpcoesActivity extends AppCompatActivity
    implements AdapterView.OnItemClickListener {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private Toolbar mToolbar;
    private ActionBarDrawerToggle mBarDrawerToggle;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opcoes);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

        mToolbar = (Toolbar) findViewById(R.id.toolbarTelaOpcoes);
        setSupportActionBar(mToolbar);

        mBarDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                mToolbar,
                R.string.app_name,
                R.string.app_name
        ){

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                supportInvalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerClosed(drawerView);
                supportInvalidateOptionsMenu();
            }

        };
        mDrawerLayout.setDrawerListener(mBarDrawerToggle);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.opcoes));
        mDrawerList.setAdapter(adapter);
       // mDrawerList.setOnClickListener(this);

        if(savedInstanceState == null){
            exibirItem(0);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_opcoes, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        atualizarTitulo(drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void exibirItem(int position) {
        String selecionado = mDrawerList.getItemAtPosition(position).toString();
        Fragment fragment = OpcoesFragment.novaInstacia(selecionado);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    private void atualizarTitulo(boolean drawerIsOpen) {
        if(drawerIsOpen) {
            mToolbar.setTitle(R.string.app_name);
        } else {
            int posicaoAtual = mDrawerList.getCheckedItemPosition();
            String opcaoAtual = mDrawerList.getItemAtPosition(posicaoAtual).toString();
            mToolbar.setTitle(opcaoAtual);
        }
    }
}

Error in the following code line

mDrawerLayout.closeDrawer(mDrawerList);

  • Try replacing this line with mDrawerLayout.closeDrawers();

1 answer

0


Instead of passing your mDrawerList as a parameter, try to pass a Gravity:

mDrawerLayout.closeDrawer(GravityCompat.START);

Or, simply use closeDrawers():

mDrawerLayout.closeDrawers();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.