简体   繁体   中英

How to call dialogfragment from another fragment?

I'm really new in android development, hope you guys can help me in my problem. I had already search for any solution but none of it works. I have 6 fragments for scrollable tabs, then each tabs has ADD TO CART buttons, if I click that button a fragment dialog should appear. In my case, there is this error.

Error:(37, 13) error: no suitable method found for show(android.support.v4.app.FragmentManager,String) method DialogFragment.show(android.app.FragmentManager,String) is not applicable (argument mismatch; android.support.v4.app.FragmentManager cannot be converted to android.app.FragmentManager) method DialogFragment.show(FragmentTransaction,String) is not applicable (argument mismatch; android.support.v4.app.FragmentManager cannot be converted to FragmentTransaction)

Heres my code by the way for the fragment to call the DialogFragment.

package info.androidhive.materialtabs.fragments;

    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import info.androidhive.materialtabs.R;


public class OneFragment extends Fragment{

public OneFragment() {
    // Required empty public constructor
}

@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
    return inflater.inflate(R.layout.fragment_one, container, false);
}

public void toDiagCartFragment(View v){
    FragmentManager manager = getFragmentManager();
    CartFragment cart = new CartFragment();
    cart.show(manager, "My Cart");
}

}

This the code for DialogFragment to be called by OneFragment

package info.androidhive.materialtabs.fragments;

  import android.app.DialogFragment;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;

  import info.androidhive.materialtabs.R;


 public class CartFragment extends DialogFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_cart2, null);
}
}

Seems like the error is in the OneFragment.java

Heres my output by the way. 这是调用对话框的片段

这应该是对话

_I really appreciate for any answers, just please be nice to me .I really don't know how to do it . :( .Thanks :)

your error says your answer clearly.

type mismatch

your dialogFragment has android.app.FragmentManager and you are calling android.support.v4.app.FragmentManager . you should use getSupportFragmentManger() insted of getFragmentManager();

first of all replace your cartFragment import to this,

import android.support.v4.app.DialogFragment;

and create this method.

public static CartFragment newInstance() {
        CartFragment dialog = new CartFragment ();
        return dialog;
    }

and in your one_fragment use like this.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();            
CartFragment newFragment = CartFragment .newInstance();
newFragment.show(ft, "My Cart");

There is a version mismatch. Instead of using getFragmentManager(), use getSupportFragmentManager() as you are using the support library version of Fragments.

Replace

FragmentManager manager = getFragmentManager();

with

FragmentManager manager = getSupportFragmentManager();

your OneFragment belongs to support library fragment while CartFragment doesn't. you need to either modify CartFragment to support library or OneFragment to default Fragment. and modifying CartFragment to support library fragment will be better as support library has more functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM