获取错误:onCreate(Bundle)与android.support.v4.app.Fragment中的onCreate(Bundle)类冲突

时间:2017-04-04 16:50:45

标签: android android-fragments fragment oncreate

我创建了一个扩展Navigation的课程Fragment。当我使用onCreate()方法时,它错误地说onCreate()类的方法与onCreate() android.support.v4.app.Fragment方法冲突。

package com.beacons.railwayaid.tablayoutapp;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.customlbs.library.IndoorsFactory;
import com.customlbs.surface.library.IndoorsSurfaceFactory;
import com.customlbs.surface.library.IndoorsSurfaceFragment;


public class Navigation extends Fragment {



@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder();
    IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder();

    indoorsBuilder.setContext(getContext());

    indoorsBuilder.setApiKey("69af5e85-007a-4df5-8715-d1c2c22e206f");



    indoorsBuilder.setBuildingId((long) 979231679);

    surfaceBuilder.setIndoorsBuilder(indoorsBuilder);

    IndoorsSurfaceFragment indoorsFragment = surfaceBuilder.build();

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(android.R.id.content, indoorsFragment, "indoors");
    transaction.commit();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab2_railway_navigation, container, false);

    return rootView;
   }
}

2 个答案:

答案 0 :(得分:1)

更改:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
}

为:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
}

答案 1 :(得分:0)

尝试将代码移动到onActivityCreated方法,如下所示:

public class Navigation extends Fragment {



@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab2_railway_navigation, container, false);

    return rootView;
   }

 @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
         IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder();
    IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder();

    indoorsBuilder.setContext(getContext());

    indoorsBuilder.setApiKey("69af5e85-007a-4df5-8715-d1c2c22e206f");



    indoorsBuilder.setBuildingId((long) 979231679);

    surfaceBuilder.setIndoorsBuilder(indoorsBuilder);

    IndoorsSurfaceFragment indoorsFragment = surfaceBuilder.build();

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(android.R.id.content, indoorsFragment, "indoors");
    transaction.commit();

   }
}