Ad

IllegalArgumentException: No View Found For Id For Fragment When Trying To SetView

it says IllegalArgumentException: No view found for id for fragment when I'm trying to setview.I know problem lies at 3rd and 4th line. it's News.java file news is a fragment.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setContentView(R.layout.fragment_news);
        listView = (ListView) getActivity().findViewById(R.id.list);

        feedItems = new ArrayList<FeedItem>();

        listAdapter = new FeedListAdapter(this, feedItems);
        listView.setAdapter(listAdapter);
Ad

Answer

Fragment.java file is look like below code :

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_news, container, false);
        //ButterKnife.bind(this, view);

       init(view);

        return view;
    }

after that in init method type below code:

private void init(View view){

listView = (ListView) view.findViewById(R.id.list);

    feedItems = new ArrayList<FeedItem>();

    listAdapter = new FeedListAdapter(this, feedItems);
    listView.setAdapter(listAdapter);
    }

also u don't forgot to extends Fragment

Ad
source: stackoverflow.com
Ad