1

I have extended fragmentActivity class , as far as I read onCreateView is available in lifecycle of fragmentActivity but its not supposed to be calling super class , but its giving me error in @override asking me to call super class

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;
}
Mr.A.N
  • 31
  • 7

4 Answers4

2
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
    return super.onCreateView(parent, name, context, attrs);
}

Not

@Override --> error Here
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)

You need you Fragments;

Step 1 - You need create one Fragment.

public class ExampleFragment extends 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;
  }

}

Step 2 - Create Fragment:

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

Step 3 - Create FragmentPagerAdapter public class PagerAdapter extends 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();
}}

Step 4 - Add FragmentPagerAdapter in your FragmentActivity:

this._viewPager.setAdapter(new PagerAdapter(this.getSupportFragmentManager()))
Peter R
  • 384
  • 1
  • 11
0

FragmentActivity doesn't actually have a method called onCreateView(LayoutInflater, ViewGroup, Bundle), I think you're confusing it with Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle) for which the default implementation only returns null in which case you don't actually need to call the super method.

FragmentActivity can act as a host for Fragments provided by the support library (regular Activity can only host android.app.Fragment). In your code you're using the SupportMapFragment which extends Fragment and overrides Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle) to provide its view to the hosting Activity.

For an Activity, you'd typically just set your button click listeners in onCreate after calling setContentView.

Zharf
  • 2,638
  • 25
  • 26
  • its given here https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html – Mr.A.N Mar 13 '17 at 13:07
  • anyway I can use onCreateView() ?? actually I'm trying to listen button events in Fragment, new to android development – Mr.A.N Mar 13 '17 at 13:14
  • @Mr.A.N events from a button in the SupportMapFragment? or do you have another fragment attached also? – Zharf Mar 13 '17 at 13:18
  • @Mr.A.N not sure how feasible that would be but I think it'd be best if you would post that as a new question with more detail about what you want to accomplish. Typically you'd extend the fragment you want to modify and override its methods, but SupportMapFragment might not be very compatible with such methods – Zharf Mar 13 '17 at 13:36
0

FragmentActivity is used to hold a Fragment prior to API 3.0. If you're targeting an app prior to Honeycomb then you should use FragmentActivity but if you're targeting an app after Honeycomb then you should use an Activity. And remember that the FragmentActivity holds a Fragment, which means that you should attach a Fragment and call OnCreateView() and other fragment methods on the Fragment and not on FragmentActivity. The way to attach a Fragment to FragmentActivity is

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

Check out this excellent SO post explaining the difference between FragmentActivity and Fragments Difference between Fragment And FragmentActivity

Community
  • 1
  • 1
Ankit Sharma
  • 663
  • 1
  • 5
  • 17
  • While it's true that FragmentActivity brings Fragment support for earlier android versions, it (and other support library things) bring also a lot of fixes and new features, such as nested fragments, for newer android versions. It's generally just better to use support library versions regardless of what android version you're targeting. – Zharf Mar 13 '17 at 13:22
0

You should try overriding onCreate and changing it from protected to public. Plus, either get rid of setContentView(R.layout.activity_maps); or change it to getActivity().setContentView(R.layout.about_main_layout);

setContentView isn't availible only in a fragment Activity's java it must have getActivity(). in front of it.

Also, you need a blank public constructor, it's required, public BlankFragment() { // Required empty public constructor }

Finally, to my knowledge onCreateView is fine, it's what's around it, so please consider the above 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); }
Walker
  • 1
  • 1