嗨我在android中有一个小应用程序,我使用带导航抽屉的片段进行菜单。但是现在我想在用户点击某些东西时在我的片段对话框弹出窗口中显示,并且我得到了这些错误:
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");
}
}
我遇到的第一个错误 fragment = new HomeFragment(); >>不兼容的类型。
HomeFragment上的第二个错误 onCreateView方法>>方法不会覆盖或实现超类型
中的方法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();
}
}
答案 0 :(得分:15)
如何修复它:
android.support.v4.app.Fragment
,android.support.v4.app.DialogFragment
,android.support.v4.app.FragmentActivity
。newFragment.show(getActivity().getSupportFragmentManager(), "dialog");
使用getsupportManager
而非FragmentManager()
FragmentManager fragmentManager = getSupportFragmentManager();
而非FragmentManager()
所有人都寻求帮助。
答案 1 :(得分:6)
HomeFragment不是片段,而是FragmentActivity ...更改将类扩展为Fragment
public class HomeFragment extends FragmentActivity { ... }