ghostrider
ghostrider

Reputation: 2238

Fragment not getting inflated

I am a beginner in android and testing the RecyclerView.Following is my Fragment:

<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="com.example.recyclerviewtest.RecyclerTestActivity" >

<android.support.v7.widget.RecyclerView
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:id="@+id/drawer"
  >

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

Following is my MainActivity.java class

package com.example.recyclerviewtest;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.StrictMode;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState == null)
    {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container, new      RecyclerTestActivity()).commit();
    if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }
}
}
}

And here is the main_activity_xml:

<LinearLayout 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="${relativePackage}.${activityClass}" 
android:orientation="horizontal">

 <fragment 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/container"      
    />
</LinearLayout>

But while attaching the fragment in the main activity in the line:

 transaction.replace(R.id.container, new     RecyclerTestActivity()).commit();

I am getting the error:

The method replace is not applicable for (int,RecyclerTestActivity)

Can someone please help me in this.

Edit: The code for RecyclerTestActivity Fragment is :

package com.example.recyclerviewtest;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class RecyclerTestActivity extends Fragment {
private Adapter adapter;
private RecyclerView recyclerview;
@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)     {
    View Layout = inflater.inflate(R.layout.activity_recycler_test,container,false);
    recyclerview=(RecyclerView)Layout.findViewById(R.id.drawer);
    adapter=new Adapter(getActivity(), getdata());
    recyclerview.setAdapter(adapter);
    recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

    return Layout;
}

public List<Information> getdata()
{
    List<Information> items = new ArrayList<>();
    int[] data ={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
    String[] content={"One","Two","Three"};

    for(int i=0;i<data.length;i++)
    {
        Information current = new Information();
        current.desc=content[i];
        current.number=data[i];
        items.add(current);
    }

    return items;

}

}

Upvotes: 2

Views: 2593

Answers (3)

Michele Lacorte
Michele Lacorte

Reputation: 5363

Use this in main_activity_xml:

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/fragmentcontainer">
</FrameLayout>

Than in MainActivity.java replace current fragment with (change YourFragment with RecyclerTestActivity) :

Fragment fr;
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fr = new YourFragment();
fragmentTransaction.replace(R.id.fragmentcontainer, fr);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();

Remeber to import:

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

YourFragment.java is a class that extends Fragment (android.app.Fragment) check here Fragment Docs

Fragment class example (Google Docs example):

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

So your code is:

MainActivity.java

package com.example.recyclerviewtest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.StrictMode;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState == null)
    {
    Fragment fr;
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fr = new  RecyclerTestActivity();
    fragmentTransaction.replace(R.id.fragmentcontainer, fr);
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    fragmentTransaction.commit();
    if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }
}
}
}

main_activity_layout.xml:

<LinearLayout 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="${relativePackage}.${activityClass}" 
android:orientation="horizontal">

    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/fragmentcontainer">
    </FrameLayout>
</LinearLayout>

Your RecyclerTestActivity.java:

package com.example.recyclerviewtest;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class RecyclerTestActivity extends Fragment {
private Adapter adapter;
private RecyclerView recyclerview;
@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)     {
    View Layout = inflater.inflate(R.layout.activity_recycler_test,container,false);
    recyclerview=(RecyclerView)Layout.findViewById(R.id.drawer);
    adapter=new Adapter(getActivity(), getdata());
    recyclerview.setAdapter(adapter);
    recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

    return Layout;
}

public List<Information> getdata()
{
    List<Information> items = new ArrayList<>();
    int[] data ={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
    String[] content={"One","Two","Three"};

    for(int i=0;i<data.length;i++)
    {
        Information current = new Information();
        current.desc=content[i];
        current.number=data[i];
        items.add(current);
    }

    return items;

}

}

Upvotes: 1

Henry
Henry

Reputation: 17851

Your problem is in this line:
transaction.replace(R.id.container, new RecyclerTestActivity()).commit();

This should be transaction.replace(R.id.container, fragment).commit(); Instead of a Fragment, you are having an Activity.

Also in your xml, you are using <fragment> tag. Instead use some layout like <FrameLayout> or <LinearLayout>

UPDATE:

In your MainActivity you are using android.app.FragmentTransaction;. But your Fragment is android.support.v4.app.Fragment;. You can't do this. You have to either completely use the AppCompat_v4 or not. Use android.app.Fragment instead.

Or I would strongly suggest your MainActivity extend FragmentActivity and you use getSupportFragmentManager() instead of getFragmentManager().

Upvotes: 2

findusl
findusl

Reputation: 2654

An Activity is no Fragment therefore you can't use it as a Fragment.. You need to create a Fragment to add it to your FragmentManager. See here.

Upvotes: 0

Related Questions