Gunaseelan
Gunaseelan

Reputation: 15515

IllegalArgumentException: No view found for id 0x7f07003c for fragment

I am new to Fragments. I tried simple fragment example. But it throws the error. I don't know what is wrong.

public class NewActivity extends Activity {
    int mStackLevel = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            Fragment newFragment = NewFragment
                    .newInstance(mStackLevel);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.simple_fragment, newFragment).commit();
        } else {
            mStackLevel = savedInstanceState.getInt("level");
        }
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("level", mStackLevel);
    }
    public static class NewFragment extends Fragment {
        int mNum;
        Context context;

        static NewFragment newInstance(int num) {
            NewFragment f = new NewFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
            context = getActivity();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.example_fragment, container,
                    false);         
            return v;
        }
    }

}

My logcat shows:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.FragmentExample/com.example.FragmentExample.NewActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07003c for fragment NewFragment{40ff0850 #0 id=0x7f07003c}

Upvotes: 2

Views: 7224

Answers (3)

tread
tread

Reputation: 11088

This answer highlights a silly mistake that may occur, that arises when nesting fragments or you are converting activities to fragments.

If you are trying to replace a fragment within a fragment with the fragmentManager but you are not inflating the parent fragment that can cause an issue.

In BaseFragment.java OnCreateView:

if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new DifferentFragment())
                    .commit();
        }

return super.onCreateView(inflater, container, savedInstanceState);

Replace super.onCreateView(inflater, container, savedInstanceState); with inflating the correct layout for the fragment:

        return inflater.inflate(R.layout.base_fragment, container, false);

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157437

ft.add(R.id.simple_fragment, newFragment).commit();

you missed setContentView for your Activity. Your Fragment can not be added without a View hierarchy.

Note : Your Fragment is hosted by an activity. So you need to set the content to the activity first.

Upvotes: 6

Dixit Patel
Dixit Patel

Reputation: 3192

Just replace this method with your method

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

        if (savedInstanceState == null) {
            Fragment newFragment = NewFragment
                    .newInstance(mStackLevel);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.simple_fragment, newFragment).commit();
        } else {
            mStackLevel = savedInstanceState.getInt("level");
        }
    }

you missed this line setContentView(Your_layout_file);

Upvotes: 1

Related Questions