Android - Navigation drawer

 
Vista:
sin imagen de perfil

Navigation drawer

Publicado por Bruno (1 intervención) el 15/01/2018 12:50:57
Tengo una clase MenuLateral que es la de Navigation Drawer, una de ContenedorFragment y SeccionAdaptador. En Seccion adaptador es donde estan los metodos que reciben los parametros para la insercción de fragmentos y titulos de la clase ContenedorFragment.
El problema es cuando compilo en Android Studio no da ningun error

Código de la clase ContenedorFragment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//Clase ContenedorFragments
 
package com.example.bruno.proyecto.Fragmentos;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.support.design.widget.TabLayout;
 
import com.example.bruno.proyecto.Adaptador.SeccionAdaptador;
import com.example.bruno.proyecto.R;
import com.example.bruno.proyecto.clases.Utilidades;
 
/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ContenedorFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ContenedorFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
 
public class ContenedorFragment extends Fragment {
private OnFragmentInteractionListener mListener;
//Declaracion de propiedades
private AppBarLayout AppBar;
private TabLayout Pestañas;
private ViewPager viewPager;
View vistas; //Puente entre referencias actividad y fragments
 
public ContenedorFragment() {
    // Required empty public constructor
}
 
/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment ContenedorFragment.
 */
// TODO: Rename and change types and number of parameters
public static ContenedorFragment newInstance(String param1, String param2) {
    ContenedorFragment fragment = new ContenedorFragment();
    Bundle args = new Bundle();
    return fragment;
}
 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
}
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //Referencias
    vistas = inflater.inflate(R.layout.fragment_contenedor, container, false);
 
    if (Utilidades.rotacion==0)
    {
        View parent = (View) container.getParent(); //casting
        //Validacion para que se instancie el appbar
        if (AppBar == null)
        {
            AppBar = (AppBarLayout) parent.findViewById(R.id.id_AppBar); // Se crea el AppBar
            Pestañas = new TabLayout (getActivity()); // Instancia para la pestaña
            //Pestañas.setTabTextColors(Color.parseColor("#FFFF"), Color.parseColor("#FFFF"));// Color para las pestañas
            AppBar.addView(Pestañas); //Se agregan las pestañas a AppBar
            viewPager = (ViewPager) vistas.findViewById(R.id.idViewPagerInformacion); //Vincula la logica de navegacion con los fragments y gestos de arraste de pantalla
            LLenarViewPager(viewPager);//Llamado al metodo que se encargar de llenar el contenedor con los fragmentos
 
            // Evento para arrastar la pantalla
            viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
            {
                // Metodo que se encarga de agregar scroll o llamado a pantalla
 
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            });
            Pestañas.setupWithViewPager(viewPager);
        }
    Pestañas.setTabGravity(TabLayout.GRAVITY_FILL);
    }else
        Utilidades.rotacion=1;
 
    return vistas;
}
 
private void LLenarViewPager(ViewPager viewPager) {
    SeccionAdaptador Adaptador = new SeccionAdaptador(getFragmentManager());
    Adaptador.addFragment(new SensorFragment(), "Puertas "); // LLama a los fragmentos para las pestañas
    Adaptador.addFragment(new MotorFragment(), "Motor "); // LLama a los fragmentos para las pestañas
    Adaptador.addFragment(new EventosFragment(), "Eventos "); // LLama a los fragmentos para las pestañas
 
    viewPager.setAdapter(Adaptador);
}
 
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}
 
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}
 
@Override
public void onDestroyView() {
    super.onDestroyView();
    if (Utilidades.rotacion==0)
    {
        AppBar.removeView(Pestañas);
    }
 
}
 
@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}
 
/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}


Código de clase MenuLateral:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Clase MenuLateral 
package com.example.bruno.proyecto;
 
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
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.view.View;
import android.widget.Button;
 
import com.example.bruno.proyecto.Fragmentos.ContenedorFragment;
import com.example.bruno.proyecto.Fragmentos.EventosFragment;
import com.example.bruno.proyecto.Fragmentos.MotorFragment;
import com.example.bruno.proyecto.Fragmentos.SensorFragment;
 
public class MenuLateral extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
        private Button mSalir;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_lateral);
    //mSalir = (Button) findViewById(R.id.Bt_Salir);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);/* Inicializa el elemento del panel lateral de navegación
    (activity_menu_lateral) */
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);/*Inicializa el elemento del panel lateral de navegación
            (activity_menu_lateral) */
    drawer.addDrawerListener(toggle);
    toggle.syncState();
 
    Fragment Menu = new SensorFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.content_menu_lateral, Menu).commit();
 
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); /*Inicializa el elemento del panel lateral de navegación
    (activity_menu_lateral) */
    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_lateral, 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();
 
    // Logica de asignacion de los fragments cuando seleciona un item
       Fragment miFragment = null;
        boolean FragmentSelccionado = false;
    if (id == R.id.idAlarma) {
        miFragment = new ContenedorFragment();
        FragmentSelccionado = true;
    } else if (id == R.id.idMonitoreo) {
        miFragment = new MotorFragment();
        FragmentSelccionado = true;
    } else if (id == R.id.idEventos) {
        miFragment = new EventosFragment();
        FragmentSelccionado = true;
    } else if (id == R.id.idHerramientas) {
 
    } else if (id == R.id.idCompartir) {
 
    } else if (id == R.id.idEnviar) {
 
    }
    // Coloca el fragments seleccionado en la pantalla principal
    if (FragmentSelccionado == true)
    {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_menu_lateral, miFragment).commit();
    }
 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}
Cuando ejecuto la aplicacion en Android Studio, y le doy click en Alarma, me da el siguiente error:

Screenshot_20180114-2139111
Screenshot_20180114-2139301
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Navigation drawer

Publicado por Miguel (2 intervenciones) el 18/01/2018 04:57:16
Para revisar tu problema se deberia tener todos los layout y los java, pero creo q tu probrema puede estar en SensorFragment(),MotorFragment() EventosFragment() ya sea en la logica incorrecta de los java de estos , o hasta podria ser en los layuot en el diseño , recuerda que los errores sintácticos dentro de " " no se reconocen
El mas minimo error e insignificante ala vista podria ser la causa
Saludis
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar