Incompatible types of fragment

自闭症网瘾萝莉.ら 提交于 2019-12-10 22:37:45

问题


Hi I have small app in android where Im using fragments with navigation drawer for menu. But now I want show in my fragments dialog popup window when user click on something and there I get these errors:

MainActivity:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

First error I get on fragment = new HomeFragment(); >>incompatible types.

Second error on HomeFragment at onCreateView method >> method does not override or implement a method from a supertype

HomeFragment:

public class HomeFragment extends FragmentActivity {

public HomeFragment(){}

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

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    final RelativeLayout rlPolievkaShowDialog=(RelativeLayout)rootView.findViewById(R.id.rlPolievkaButton);
    rlPolievkaShowDialog.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        }

    });
    return rootView;
}

private void showDialog() {
    DialogFragment newFragment = DialogFragmentAlergeny.newInstance();
    newFragment.show(getSupportFragmentManager(), "dialog");
}

}

DialogFragmentAlergeny:

public class DialogFragmentAlergeny extends DialogFragment {

public static DialogFragmentAlergeny newInstance() {
    DialogFragmentAlergeny frag = new DialogFragmentAlergeny();
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    View view = getActivity().getLayoutInflater().inflate(R.layout.alergeny_dialog, null);
    alertDialogBuilder.setView(view);
    alertDialogBuilder.setTitle(getString(R.string.alergeny_dialog_title));
    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
        }
    });
    return alertDialogBuilder.create();
}

}


回答1:


HomeFragment is not a fragment but a FragmentActivity...change extends class to Fragment

public class HomeFragment extends FragmentActivity { ... }



回答2:


How Fix it:

  1. HomeFragment must extend Fragment
  2. You must use/import android.support.v4.app.Fragment, android.support.v4.app.DialogFragment, android.support.v4.app.FragmentActivity everywhere where needed.
  3. newFragment.show(getActivity().getSupportFragmentManager(), "dialog"); use getsupportManager not FragmentManager()
  4. Use FragmentManager fragmentManager = getSupportFragmentManager(); not FragmentManager()

Thats all thx all for help.



来源:https://stackoverflow.com/questions/21475581/incompatible-types-of-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!