0

Estoy tratando de insertar un fragment dentro de un Navigation Drawer, pero al momento de ejecutarlo me dice la aplicacion que se detuvo y checando los errores me dice que la causa es:

Caused by: java.lang.RuntimeException: com.example.aguil.visionecologica.Clases_Principales.Lateral@719b5ce must implement OnFragmentInteractionListener

El codigo de mi Navigation Drawer donde hago la conexión con los fragments

package com.example.aguil.visionecologica.Clases_Principales;
public class Lateral extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

    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.addDrawerListener(toggle);
    toggle.syncState();

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

    Cargar(new Prin_Eco());
}




@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.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();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

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

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

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

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

    }

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

private void Cargar(Fragment fragmento){
    FragmentManager manager=getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.contendor_principal, fragmento).commit();
    }
}

El codigo XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/toolbarview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_lateral" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/contendor_principal"
    android:layout_below="@id/toolbarview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>

el LogCat

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.aguil.visionecologica, PID: 30735
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aguil.visionecologica/com.example.aguil.visionecologica.Clases_Principales.Lateral}: java.lang.RuntimeException: com.example.aguil.visionecologica.Clases_Principales.Lateral@719b5ce must implement OnFragmentInteractionListener
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6123)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
               Caused by: java.lang.RuntimeException: com.example.aguil.visionecologica.Clases_Principales.Lateral@719b5ce must implement OnFragmentInteractionListener
                  at com.example.aguil.visionecologica.Fragments.Prin_Eco.onAttach(Prin_Eco.java:84)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)
                  at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1190)
                  at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1073)
                  at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2407)
                  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2365)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2272)
                  at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3271)
                  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3227)
                  at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:610)
                  at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1270)
                  at android.app.Activity.performStart(Activity.java:6689)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6123) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
1
  • 1
    Debes de implementarlo en el Activity donde lo llamas. Por ejemplo "public class tuclase implements nombredetufragment.OnFragmentInteractionListener"
    – Alfredo
    Commented el 27 sept. 2018 a las 16:55

1 respuesta 1

1

Tendrías que sobreescribir el método OnFragmentInteractionListener en la misma actividad que implementa el fragment. Puedes dejarlo vacío:

@Override
public void onFragmentInteraction(Uri uri){
}

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.