不兼容的类型片段 [英] Incompatible types of fragment

查看:150
本文介绍了不兼容的类型片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有Android的小的应用程序,其中进出口使用的片段导航抽屉菜单。但现在我想表现出我的片段对话框弹出窗口时一些用户点击那里我得到这些错误:

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");
    }
}

第一个错误我得到的片段=新HomeFragment(); >>不兼容的类型

在HomeFragment二错误在 onCreateView方法 >>方法不覆盖或从超实现的方法

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();
}

}

推荐答案

HomeFragment不是一个片段,但一个FragmentActivity ...变化扩展类片段

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

public class HomeFragment extends FragmentActivity { ... }

这篇关于不兼容的类型片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆