[英]Which fragmentManager Should i use
Im using fragments and tryng to make a form which i can get the date using datepicker for after store it in a database (There is not the problem now) The problem its i was following a guide to learn how to use the datepicker in fragments activity.我使用片段并尝试制作一个表格,我可以在将其存储在数据库中后使用 datepicker 获取日期(现在没有问题)我正在遵循指南学习如何在片段活动中使用日期选择器的问题. And i got a pair of questions (And errors) I've been using This guide
我有一对问题(和错误)我一直在使用本指南
And im getting this error:我收到这个错误:
"The method getSupportFragmentManager is undefined for the type"
“该类型的 getSupportFragmentManager 方法未定义”
i've read there is not getSupportFragmentManager in "Fragments" but they are in "FragmentActivity"我读过“片段”中没有 getSupportFragmentManager 但它们在“FragmentActivity”中
There is a equivalent in "Fragments" For this method?在“片段”中有一个等效的方法吗? or what i should do.
或者我应该怎么做。
I was searching and reading a lot of guides but this is the easily way that i found and have this problem.我正在搜索和阅读很多指南,但这是我发现并遇到此问题的简单方法。
I will post my Fragment class,fragment xml,and datepickerfragment我将发布我的 Fragment 类、fragment xml 和 datepickerfragment
FragmentClass (regventa_Fragment).. FragmentClass (regventa_Fragment)..
public class Regventa_Fragment extends Fragment {
public Button guardar;
public Button fecha_button;
public Regventa_Fragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_regventa, container, false);
Toast.makeText(getActivity(), "Hola", Toast.LENGTH_SHORT).show();
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
guardar=(Button)getView().findViewById(R.id.guardar);
//fecha_button=(Button)getView().findViewById(R.id.fecha_button);
getView().findViewById(R.id.fecha_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDatePicker();
}
private void showDatePicker() {
// TODO Auto-generated method stub
}
});
}
private void showDatePicker() {
DatePickerFragment date = new DatePickerFragment();
/**
* Set Up Current Date Into dialog
*/
Calendar calender = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
/**
* Set Call back to capture selected date
*/
date.setCallBack(ondate);
date.show(getFragmentManager(), "Date Picker");
}
OnDateSetListener ondate = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
}
};
} }
fragment_regventa.XML fragment_regventa.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/guardar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/guardar" />
<Button
android:id="@+id/borrar"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/borrar" />
</LinearLayout>
<Button
android:id="@+id/fecha_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fecha_venta"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/fecha_button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/regventa"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/comprador"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/comprador" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/fecha_venta"
style="android:editTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fecha_button"
android:layout_below="@+id/comprador"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="@string/fecha_venta"
android:textAlignment="center"
android:textSize="17sp" />
<EditText
android:id="@+id/elevendidos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pesoventa"
android:layout_below="@+id/pesoventa"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/elementos_vendidos"
android:inputType="number" />
<EditText
android:id="@+id/pesoventa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/comprador"
android:layout_below="@+id/fecha_button"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="@string/pesoventa" />
DatePicker Fragmentent日期选择器片段
public class DatePickerFragment extends DialogFragment {
OnDateSetListener ondateSet;
public DatePickerFragment() {
}
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
private int year, month, day;
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
}
If i change from "Fragment" to "FragmentActivity" Thats going to works good?如果我从“Fragment”更改为“FragmentActivity”,效果会好吗? with the other fragments i have?.. Thanks
和我有的其他片段一起吗?..谢谢
getSupportFragmentManager()
is meant for android support library getSupportFragmentManager()
用于 android 支持库
that means这意味着
if you are using support library you have to use getSupportFragmentManager()
else you have to use getFragmentManager()
如果您使用支持库,则必须使用
getSupportFragmentManager()
否则必须使用getFragmentManager()
to verify check your import验证检查您的导入
import android.support.v4.app.Fragment;
means you using support library then you have to use getSupportFragmentManager()
意味着您使用支持库然后您必须使用
getSupportFragmentManager()
import android.app.Fragment
means you are using the native library then you have to use getFragmentManager()
import android.app.Fragment
意味着您正在使用本机库然后您必须使用getFragmentManager()
Good Luck :)祝你好运 :)
I solve the problem.. I was using我解决了问题..我正在使用
import android.support.v4.app.Fragment
Instead of...代替...
import android.app.Fragment;
In DatepickerFragment.在 DatepickerFragment 中。
After that i use...之后我用...
date.show(getActivity().getFragmentManager(), "Date Picker");
Thanks谢谢
First you need to understand the deference between FragmentActivity & Fragment.首先你需要了解 FragmentActivity 和 Fragment 之间的区别。 FragmentActivity is subclass of Activity & activity holds Fragment & two are different things.
FragmentActivity 是 Activity 的子类,activity 持有 Fragment & 两个是不同的东西。 See the below links & look that fragment doesn't not inherit from Activity.
请参阅以下链接并查看该片段不是从 Activity 继承的。
So, if you need to use getSupportFragmentManager() then first call the activity that holds the fragment & call the fragmentManager.因此,如果您需要使用 getSupportFragmentManager() 然后首先调用保存片段的活动并调用 fragmentManager。 like :
getActivity().getSupportFragmentManager()
像:
getActivity().getSupportFragmentManager()
Another thing is if you use Fragment from support library then you get " getSupportFragmentManager()
" other wise you get " getFragmentManager()
"另一件事是,如果您使用支持库中的 Fragment,那么您将获得“
getSupportFragmentManager()
”, getFragmentManager()
您将获得“ getFragmentManager()
”
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.