getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);

266 views Asked by At

pardon me if this is a stupid error. there are two fragments in my activity layout (xml) and in order to let them communicate i am trying to get the instance of one fragment inside my main activity. i have done it earlier but this time it throws erro (required videolisting fragment but found fragment) i am not sure what i have given wrong. might be just a silly mistake. can you please assist. Thanks in advance.

below is code snippet:

public class VideoListingActivity extends ActionBarActivity implements FilterFragment.OnFilterItemSelectedListener{

 public VideoListFragment videoListFragment;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_listing);

    videoListFragment =(VideoListFragment) getSupportFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);

}

below is the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="my activity context">


    <fragment
        android:layout_width="match_parent"
        android:layout_height="@dimen/filter_fragment_height"
        android:name="com.text1.text2.text3.pkg.FilterFragment"
        android:id="@+id/frg_video_listing_filter_fragment"
        ></fragment>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.text1.text2.text3.pkg.VideoListFragment"
        android:id="@+id/frg_video_listing_video_listing_fragment"
        ></fragment>

</RelativeLayout>

Below is fragment class code

package com.rrdtech.vidyavaan.android.Fragments;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rrdtech.vidyavaan.android.R;


public class VideoListFragment extends ListFragment {
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onStart() {
    super.onStart();
}
}

Below is stack trace

Error:(22, 91) error: inconvertible types
required: VideoListFragment
found:    Fragment
2

There are 2 answers

0
Thomas R. On

Maybe you need to add the class attribute in XML.

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.text1.text2.text3.pkg.VideoListFragment"
    class="com.text1.text2.text3.pkg.VideoListFragment"
    android:id="@+id/frg_video_listing_video_listing_fragment"/>
3
Bharatesh On

You Need to know

  1. You are using support library for Fragment using getSupportFragmentManager(), In this case you should always use support package classes.
  2. VideoListFragment extending android.app.ListFragment; instead of android.support.v4.app.ListFragment; (this thing causes the error). And make sure fragment layout has ListView with "@android:id/list" id.