Like using buttons from a Fragmen like opening other Fragments

0

I have a Frangent class to which I am trying to put ImageButton buttons so that when choosing one of them I show another Fragment class

This is the code with which I'm testing now, which does not do anything, well, when you click on the icons the application closes.

public class Menu_6 extends Fragment implements View.OnClickListener{

Button botonRetraso1;
Button botonRetraso2;

private Context context;

public Menu_6() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate( R.layout.menu_6, container, false );

    botonRetraso1 = rootView.findViewById(R.id.btRetraso1);
    botonRetraso2 = rootView.findViewById(R.id.btRetraso2);

    botonRetraso1.setOnClickListener(this);
    botonRetraso2.setOnClickListener(this);

    return rootView;
}

public void onClick(View v) {
    switch (v.getId()){
        case R.id.btRetraso1:
            cargarFragment(new Retrasos1());
            break;

        case R.id.btRetraso2:
            cargarFragment(new Retrasos2());
            break;
    }
}

private void cargarFragment(Fragment fragment) {
    FragmentTransaction ft =  getActivity().getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
}

}

Notes:

  • I removed the error message, because now it shows none
  • I've changed ImageButton to Button , from the code buttonRelay1 = (Button) rootView.findViewById (R.id.btRelay1); I've deleted (Button) it seems to be unnecessary.

Code of Delays1.java

public class Retrasos1 extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;

WebView mWebView;

private OnFragmentInteractionListener mListener;

public Retrasos1() {
}

public static Retrasos newInstance(String param1, String param2) {
    Retrasos1 fragment = new Retrasos1();
    Bundle args = new Bundle();
    args.putString( ARG_PARAM1, param1 );
    args.putString( ARG_PARAM2, param2 );
    fragment.setArguments( args );
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );

    if (getArguments() != null) {
        mParam1 = getArguments().getString( ARG_PARAM1 );
        mParam2 = getArguments().getString( ARG_PARAM2 );
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate( R.layout.retrasos1, container, false );

    mWebView = view.findViewById(R.id.webView1 );
    mWebView.getSettings().setJavaScriptEnabled( true );
    mWebView.getSettings().setAppCacheEnabled( true );

    mWebView.getSettings().setBuiltInZoomControls( true );
    mWebView.getSettings().setDisplayZoomControls( false );
    mWebView.getSettings().setSupportZoom( true );
    mWebView.getSettings().setDefaultZoom( WebSettings.ZoomDensity.FAR );
    mWebView.getSettings().setLoadWithOverviewMode( true );
    mWebView.getSettings().setUseWideViewPort( true );
    mWebView.setInitialScale(1);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mWebView.setScrollbarFadingEnabled(false);

    mWebView.loadUrl( "https://www.google.es/" );

    return view;
}



public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}


@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}

}

delays1.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.SoCu.CirDocu.Retrasos1">

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

I would appreciate it if you can help me with the code to open a Fragment from another Fragment using buttons

Greetings.

    
asked by SoCu 29.08.2018 в 20:51
source

3 answers

1

The problem is that your context attribute is in null

Intent detail = new Intent(context.getApplicationContext(), Retraso1.class);
context.startActivity(detail);

Because you do not try this way

Intent detail = new Intent(getActivity(), Retraso1.class);
getActivity().startActivity(detail);
    
answered by 29.08.2018 в 21:09
1

Just as you have it is to open Activities , from a fragment , and Activities must be declared in Manifest .

To open a fragment you use fragment transaction , you do not need to declare them in the manifest. And the same is used to open a fragment , starting with a Activity . In this case with a onClick :

  public void onClick(View v) {
        Retraso1 fragment = new Retraso1();  // Retraso1 es el nombre del fragment
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();  // si usas import android.support.v4.app.Fragment;
        //FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();       // si usas import android.app.Fragment;
        ft.replace(R.id.frame_container, fragment, "retraso1fragment");    // frame es el id del FrameLayout que va contener los fragments
        ft.addToBackStack(null);                              // para agregarlo a la pila
        ft.commit();
      }
    });

In case of several buttons, with switch , it is better to create a transaction method:

 public void onClick(View v) {
  switch (v.getId()){
   case R.id.btRetraso1:
     cargarFragment(new Retraso1());
     break;

   case R.id.btRetraso2:
     cargarFragment(new Retraso2());
     break;
    }
 }

   private void cargarFragment(Fragment fragment) {
      FragmentTransaction ft =  getActivity().getSupportFragmentManager().beginTransaction();
      ft.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
 }

To open a Activity , from another Activity we use intent :

 Intent intent = new Intent(ActivityEnLaQueEstas.this, LaOtraActivity.class);
 startActivity(intent);
    
answered by 29.08.2018 в 23:15
1

Hello to move from one Fragment to another is different from one class to another.

botonRetraso1 .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Retraso1= new Frg_Retraso1 ();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.content_main, Retraso1);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });

** and in the upper part call the Fragment that you want to communicate: Frg_Delay1 Delay1;

** In the R.id.content_main it will always point to your Main, because it is the main one, so your XML of your Main Main should put an ID like this android: id="@ + id / content_main ", and ready

And another little thing I do not recommend you use a Button Image using a normal button, you can add an equal image, it's much easier.

    
answered by 30.08.2018 в 00:05