gpt4 book ai didi

android - 不兼容的 fragment 类型

转载 作者:太空狗 更新时间:2023-10-29 15:30:06 24 4
gpt4 key购买 nike

您好,我在 android 中有一个小应用程序,我在其中使用带抽屉导航的 fragment 作为菜单。但是现在我想在用户点击某些东西时在我的 fragment 对话框弹出窗口中显示,并且出现这些错误:

主要 Activity :

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

我遇到的第一个错误是 fragment = new HomeFragment(); >>incompatible types

onCreateView 方法 上 HomeFragment 的第二个错误 >> 方法没有覆盖或实现父类(super class)型的方法

主页 fragment :

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 必须扩展 Fragment
  2. 您必须使用/导入 android.support.v4.app.Fragmentandroid.support.v4.app.DialogFragmentandroid.support.v4 .app.FragmentActivity 随处可见。
  3. newFragment.show(getActivity().getSupportFragmentManager(), "dialog"); 使用 getsupportManager 而不是 FragmentManager()
  4. 使用 FragmentManager fragmentManager = getSupportFragmentManager(); 而不是 FragmentManager()

这就是所有的帮助。

关于android - 不兼容的 fragment 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21475581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com