EGHDK
EGHDK

Reputation: 18130

Trying to create a fragment class

I'm trying to create a fragment class following from this:

And they give you the code for a fragment class, but I'm getting

ExampleFragments.java

package com.example.learn.fragments;

public static class ExampleFragments extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.blue_pill_frag, container, false);
    }
}

But I'm getting an error on line 9, where I declare the name of the class.

Illegal modifier for the class ExampleFragments; only public, abstract & final are permitted

I'm sure it's something basic that I'm not understanding, thanks.

Upvotes: 1

Views: 1295

Answers (2)

Iñigo
Iñigo

Reputation: 12823

In the example you're following, the static modifier is used because the Fragment is a nested class.

To get rid of the error you can include that fragment into an existing class, or remove the static modifier.

Upvotes: 1

aioobe
aioobe

Reputation: 420991

You can't have a static top-level class. Change

public static class ExampleFragments extends Fragment {

to

public class ExampleFragments extends Fragment {

Upvotes: 4

Related Questions