Daniel Kobe
Daniel Kobe

Reputation: 9825

Unreachable statement instantiating an instance of a fragment class inside of fragment

I have a fragment called ProfilePicBtn and Im trying to load it in my fragment class called HomeFragment. But when I try and instantiate an instance of ProfilePicBtn I get an error saying unreachable statement. Note Im trying to follow the developer docs here.

package com.example.adam.hilo;

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

public class HomeFragment extends Fragment {
    public HomeFragment() {
    }

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

        Fragment profileBtnFragment = new ProfileBtnFragment();
    }
}

Upvotes: 0

Views: 189

Answers (2)

Shishupal Shakya
Shishupal Shakya

Reputation: 1672

Change order of statements of onCreateView() .

    public class HomeFragment extends Fragment {
    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_home, container, false);
        Fragment profileBtnFragment = new ProfileBtnFragment();
        return view ;
    }
}

Upvotes: 0

dmolony
dmolony

Reputation: 1135

This code has an unconditional return statement before the line which allocates the new ProfileBtnFragment. So the second line is unreachable.

Upvotes: 1

Related Questions