Running a Popup on the default button of the Drawer Layout

1

I am with the help of a video tutorial that I saw on youtube, the app compiles well but when you enter the activity where the menu is and that floating button occurs this error

FATAL EXCEPTION: main
Process: company.viral.organizadorjec, PID: 16485
java.lang.RuntimeException: Unable to start activity ComponentInfo{company.viral.organizadorjec/company.viral.organizadorjec.MenuCentral}: java.lang.ClassCastException: android.support.v4.widget.DrawerLayout cannot be cast to android.widget.RelativeLayout

I add the classes involved in this dilemma

MENU CENTRAL.class (class where the menu and the button are)

    package company.viral.organizadorjec;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import company.viral.organizadorjec.Fragment.AjustesF;
import company.viral.organizadorjec.Fragment.CalendarioF;
import company.viral.organizadorjec.Fragment.ConfiguracionActividadF;
import company.viral.organizadorjec.Fragment.ConfiguracionMateriaF;
import company.viral.organizadorjec.Fragment.ConfiguracionPeriodoF;
import company.viral.organizadorjec.Fragment.ConfiguracionProfesorF;
import company.viral.organizadorjec.Fragment.InicioF;
import company.viral.organizadorjec.Fragment.PerfilF;
import company.viral.organizadorjec.Fragment.ProfesoresF;

public class MenuCentral extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private PopupWindow popupadicion;
    private DrawerLayout posicionpopup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_central);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        posicionpopup = (DrawerLayout) findViewById(R.id.drawer_layout);

        //colocamos el fragment con que inicia el menu

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.contenedor,new InicioF()).commit();


        //este es el apartado para el botonsito flotante

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {

            //metodo de escucha para el popup
            @Override
            public void onClick(View view) {

                //implementamos el popup
                LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                final View vistaadicion = inflater.inflate(R.layout.activity_pop_adicion,null);

                popupadicion = new PopupWindow(
                        vistaadicion, RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT
                );

                //luego de clicear y abrir el popup le decimos...
                //si das al profe ve a profe
                LinearLayout btnprofe = (LinearLayout) vistaadicion.findViewById(R.id.btnagregarprofesor);
                btnprofe.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        FragmentManager fragmentManager = getSupportFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionProfesorF()).commit();
                        popupadicion.dismiss();

                    }
                });
                //si le das actividad ve actividad
                LinearLayout btnactividad = (LinearLayout) vistaadicion.findViewById(R.id.btnagregaractividad);
                btnactividad.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        FragmentManager fragmentManager = getSupportFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionActividadF()).commit();
                        popupadicion.dismiss();

                    }
                });
                //si le das a materias ve a materias
                LinearLayout btnmaterias = (LinearLayout) vistaadicion.findViewById(R.id.btnagregarmateria);
                btnmaterias.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        FragmentManager fragmentManager = getSupportFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionMateriaF()).commit();
                        popupadicion.dismiss();

                    }
                });
                //si le das a periodo ve a periodo
                LinearLayout btnperiodo = (LinearLayout) vistaadicion.findViewById(R.id.btnagregarperiodo);
                btnperiodo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        FragmentManager fragmentManager = getSupportFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionPeriodoF()).commit();
                        popupadicion.dismiss();

                    }
                });
                //luego le decimos que cierre el popup con el boton

                Button cerrarboton = (Button) vistaadicion.findViewById(R.id.btnpopupcerrar);
                cerrarboton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupadicion.dismiss();
                    }
                });
                //hubicamos donde queremos el popup
                popupadicion.showAtLocation(posicionpopup, Gravity.CENTER,0,0 );
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_central, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        FragmentManager fragmentManager = getSupportFragmentManager();

        if (id == R.id.nav_camera) {
            fragmentManager.beginTransaction().replace(R.id.contenedor,new InicioF()).commit();

        } else if (id == R.id.nav_gallery) {
            fragmentManager.beginTransaction().replace(R.id.contenedor,new ProfesoresF()).commit();

        } else if (id == R.id.nav_slideshow) {
            fragmentManager.beginTransaction().replace(R.id.contenedor,new CalendarioF()).commit();

        } else if (id == R.id.nav_manage) {
            fragmentManager.beginTransaction().replace(R.id.contenedor,new AjustesF()).commit();

        } else if (id == R.id.nav_share) {
            fragmentManager.beginTransaction().replace(R.id.contenedor,new PerfilF()).commit();

        } else if (id == R.id.nav_send) {

        } else if (id == R.id.nav_materia){
            fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionMateriaF()).commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

activity_pop_adition.xml (this is the layout involved for the popup)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#64ffffff"
    android:layout_height="match_parent">

    <TextView
        android:text="¿Que desea adicionar?"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"
        android:id="@+id/textView3" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/btnagregarprofesor">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="52dp"
                android:src="@drawable/ic_action_maps_local_library"
                android:id="@+id/imageView2"
                android:layout_weight="1" />

            <TextView
                android:text="Profesor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView4"
                android:gravity="center"
                android:textSize="24sp"
                android:layout_weight="1" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/btnagregaractividad">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="52dp"
                android:src="@drawable/ic_action_action_note_add"
                android:id="@+id/imageView3"
                android:layout_weight="1" />

            <TextView
                android:text="Actividad"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView5"
                android:gravity="center"
                android:textSize="24sp"
                android:layout_weight="1" />
        </LinearLayout>

    </LinearLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnagregarmateria">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="52dp"
                android:src="@drawable/ic_action_action_event"
                android:id="@+id/imageView6" />

            <TextView
                android:text="Materia"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView7"
                android:gravity="center"
                android:textSize="24sp"
                android:layout_weight="1" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnagregarperiodo">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="52dp"
                android:src="@drawable/ic_action_action_event"
                android:id="@+id/imageView8" />

            <TextView
                android:text="Periodo de Actividad"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView9"
                android:gravity="center"
                android:textSize="24sp"
                android:layout_weight="1" />

        </LinearLayout>
    </LinearLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:text="Cancelar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/btnpopupcerrar" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></LinearLayout>
    </LinearLayout>

</LinearLayout>

activity_menu_central

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <include
        layout="@layout/app_bar_menu_central"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_menu_central"
        app:menu="@menu/activity_menu_central_drawer" />

</android.support.v4.widget.DrawerLayout>
    
asked by ERny JOsé HIdalgo COrrea 06.12.2016 в 20:00
source

1 answer

2

The error is described as:

  

DrawerLayout can not be cast to android.widget.RelativeLayout

The cast you are performing is incorrect:

posicionpopup = (RelativeLayout) findViewById(R.id.drawer_layout);

should be:

posicionpopup = (DrawerLayout) findViewById(R.id.drawer_layout);

and obviously define the variable DrawerLayoutposicionpopup of type DrawerLayout

 private DrawerLayoutposicionpopup;
    
answered by 06.12.2016 / 20:56
source