问题
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:
- HomeFragment must extend Fragment
- You must use/import
android.support.v4.app.Fragment
,android.support.v4.app.DialogFragment
,android.support.v4.app.FragmentActivity
everywhere where needed. newFragment.show(getActivity().getSupportFragmentManager(), "dialog");
usegetsupportManager
notFragmentManager()
- Use
FragmentManager fragmentManager = getSupportFragmentManager();
notFragmentManager()
Thats all thx all for help.
来源:https://stackoverflow.com/questions/21475581/incompatible-types-of-fragment