我已经扩展了fragmentActivity类,只要我读到onCreateView在fragmentActivity的生命周期中可用,但它不应该调用超类,但它在@override中给我错误,要求我调用超类
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Initializing
MarkerPoints = new ArrayList<>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override --> error Here
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps,
container, false);
Button button = (Button) view.findViewById(R.id.reportButton);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Report Button Works",Toast.LENGTH_SHORT).show();
}
});
return view;
}
答案 0 :(得分:1)
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return super.onCreateView(parent, name, context, attrs);
}
不
@Override --> error Here
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
你需要你的碎片;
第1步 - 您需要创建一个片段。
公共类ExampleFragment扩展了Fragment {
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
//Create the view from XML layout.
final View view = inflater.inflate(R.layout.fragment_example, null);
//Perform additional configuration on layout components here.
return view;
}
}
第2步 - 创建片段:
public class Fragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
}}
第3步 - 创建FragmentPagerAdapter 公共类PagerAdapter扩展了FragmentPagerAdapter {
private List<Fragment> fragmentList;
private Fragment1 fragment1;
public PagerAdapter(FragmentManager fm) {
super(fm);
this.fragment1 = new Fragment1();
this.fragmentList = new ArrayList<>();
this.fragmentList.add(this.fragment1);
}
@Override
public Fragment getItem(int position) {
//Get fragment in list position
return fragmentList.get(position);
}
@Override
public int getCount() {
//return size of fragments list.
return fragmentList.size();
}}
第4步 - 在FragmentActivity中添加FragmentPagerAdapter:
this._viewPager.setAdapter(new PagerAdapter(this.getSupportFragmentManager()))
答案 1 :(得分:0)
FragmentActivity
实际上没有一个名为onCreateView(LayoutInflater, ViewGroup, Bundle)
的方法,我认为你将它与Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
混淆,默认实现只返回null,在这种情况下你不会实际上需要调用super方法。
FragmentActivity
可以充当支持库提供的Fragments
的主机(常规Activity
只能托管android.app.Fragment
)。在您的代码中,您使用SupportMapFragment
扩展Fragment
并覆盖Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
以向托管活动提供其视图。
对于某个活动,您通常只需在调用onCreate
后在setContentView
中设置按钮点击监听器。
答案 2 :(得分:0)
FragmentActivity
用于在Fragment
之前保留API 3.0
。如果您在Honeycomb
之前定位应用,则应使用FragmentActivity
,但如果您在Honeycomb
之后定位应用,则应使用Activity
}。请记住,FragmentActivity
持有一个Fragment
,这意味着您应该在Fragment
上附加OnCreateView()
并调用Fragment
和其他片段方法{1}}而不是FragmentActivity
。
将Fragment
附加到FragmentActivity
的方法是
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(CONTENT_VIEW_ID, myFragment).commit();
查看这篇优秀的SO帖子,解释FragmentActivity
和Fragments
之间的区别
Difference between Fragment And FragmentActivity
答案 3 :(得分:0)
您应该尝试覆盖onCreate并将其从protected更改为public。 另外,要么摆脱setContentView(R.layout.activity_maps);或者将其更改为getActivity()。setContentView(R.layout.about_main_layout);
setContentView只能在片段Activity的java中使用,它必须具有getActivity()。在它面前。
此外,您还需要一个空白的公共构造函数,public BlankFragment() {
// Required empty public constructor
}
最后,据我所知,关于创建视图很好,它是什么,所以请考虑上面的posibilites。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.about_main_layout);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Initializing
MarkerPoints = new ArrayList<>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); }