我怎么能这样说:
public class TagsDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.tags_dialog,null)); builder.setMessage("This is a message").setTitle("TAGS"); return builder.create(); } }
从ViewPager里面的Fragment里面:
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { ... ImageView btnTags = (ImageView)view.findViewById(R.id.btnTags); btnTags.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DialogFragment dlg = new TagsDialog(); //this line doesn't compile dlg.show(getSupportFragmentManager(),"tags"); } }); } }
我已经尝试了很长时间才能使这个工作,但getSupportFragmentManager永远不会解决…任何想法?
编辑:
我觉得这都是由支持FragmentManager和android.app.FragmentManager引起的,但我不知道如何解决这个问题,因为我正在使用支持库中的ViewPager …
getSupportFragmentManager /以及所有相关的getFragManager方法(如父级和子级)总是从支持lib返回Manager,而show方法需要核心方法.
进口是:
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; FragmentManager fm = getActivity().getSupportFragmentManager(); // returns from support lib DialogFragment dlg = new TagsDialog(); dlg.show(fm,"tags"); // wants core...
如果我只使用core,则getActivity()上不存在getSupportFragmentManager()…