【问题标题】:onCreateView() method asking for super class callonCreateView() 方法请求超类调用
【发布时间】:2017-03-13 12:54:14
【问题描述】:

我已经扩展了 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;
}

【问题讨论】:

  • 错误是什么?日志? IDE?

标签: java android


【解决方案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 步 - 您需要创建一个 Fragment。

公共类 ExampleFragment 扩展片段 {

  @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()))

【讨论】:

  • 你能解释一下为了让它工作需要传递的参数吗?
【解决方案2】:

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) 以将其视图提供给托管活动。

对于 Activity,您通常只需在调用 setContentView 后在 onCreate 中设置按钮点击侦听器即可。

【讨论】:

  • 无论如何我都可以使用 onCreateView() ??实际上我正在尝试在 Fragment 中侦听按钮事件,这是 android 开发的新手
  • @Mr.A.N 来自 SupportMapFragment 中的按钮的事件?还是您还附加了另一个片段?
  • @Mr.A.N 不确定这有多可行,但我认为最好将其作为一个新问题发布,并详细说明您想要完成的工作。通常,您会扩展要修改的片段并覆盖其方法,但 SupportMapFragment 可能与此类方法不太兼容
【解决方案3】:

您应该尝试覆盖 onCreate 并将其从受保护更改为公开。 另外,要么摆脱 setContentView(R.layout.activity_maps);或将其更改为 getActivity().setContentView(R.layout.about_main_layout);

setContentView 不能仅在片段 Activity 的 java 中可用,它必须具有 getActivity()。在它面前。

另外,你需要一个空白的公共构造函数,它是必需的,public BlankFragment() { // Required empty public constructor }

最后,据我所知 onCreateView 很好,它就是它周围的东西,所以请考虑上述可能性。

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

【讨论】:

    【解决方案4】:

    FragmentActivity 用于在API 3.0 之前保存Fragment。如果您在Honeycomb 之前定位应用程序,那么您应该使用FragmentActivity,但如果您在Honeycomb 之后定位应用程序,那么您应该使用Activity。请记住,FragmentActivity 拥有 Fragment,这意味着您应该在Fragment 上而不是FragmentActivity 上附加Fragment 并调用OnCreateView() 和其他片段方法. 将Fragment 附加到FragmentActivity 的方法是

    android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(CONTENT_VIEW_ID, myFragment).commit();
    

    查看这篇优秀的 SO 帖子,解释 FragmentActivityFragments 之间的区别 Difference between Fragment And FragmentActivity

    【讨论】:

    • 虽然 FragmentActivity 确实为早期的 android 版本带来了 Fragment 支持,但它(和其他支持库的东西)也为较新的 android 版本带来了许多修复和新功能,例如嵌套片段。无论您的目标是什么 android 版本,通常最好使用支持库版本。
    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 2012-12-30
    • 2018-08-29
    • 2011-10-24
    • 2017-11-09
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多