1

Estoy con la ayuda de un video tutorial que vi en youtube, la app compila bien pero al momento de entrar a la actividad donde se encuentra el menu y dicho boton flotante ocurre este 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

adiciono las clases implicadas en dicho dilema

MENU CENTRAL.class (clase donde esta el menu y el boton)

    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 (este es el layout implicado para el 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>

1 respuesta 1

2

El error esta descrito como:

DrawerLayout cannot be cast to android.widget.RelativeLayout

El cast que estas realizando es incorrecto:

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

debe ser:

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

y obviamente definir la variable DrawerLayoutposicionpopup de tipo DrawerLayout

 private DrawerLayoutposicionpopup;
9
  • o por dios !! de nuevo tu? de verdad dejame dialogar contigo en privado xD (probando solucion ) Commented el 6 dic. 2016 a las 20:00
  • 1
    Pues agrega un Chat Erny
    – Jorgesys
    Commented el 6 dic. 2016 a las 20:00
  • como se hace eso ? o.O Commented el 6 dic. 2016 a las 20:03
  • 1
    WUAA!!! LO MEJOR DEL MUNDO ESO ERA!! o.O Commented el 6 dic. 2016 a las 20:06
  • 1
    @Elenasys Me has ganado... ¡Yeeei! ( ᐛ )و
    – x4mp73r
    Commented el 6 dic. 2016 a las 20:10

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.