Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String)

Tags:

android

Im trying to Use Fragements. But in MainActivity.java file the transaction.add(R.id.my_layout, testfrag, "");not Initiated correctly.please find below the code i used.

package com.example.testtabs;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.support.v4.app.Fragment;
import com.example.testtabs.R;



public class MainActivity extends TabActivity {


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

         FragementTest testfrag=new FragementTest();
        FragmentManager manager=getFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.add(R.id.my_layout, testfrag, "");//Error is here

    }
}

activity_main.xml :

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:id="@+id/my_layout"
  >

Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String)

Fragement.test

package com.example.testtabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class FragementTest extends Fragment {

    public FragementTest() {
        // Required empty public constructor
    }

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

}
like image 797
VenushkaT Avatar asked Feb 12 '14 08:02

VenushkaT


People also ask

How to add fragment to XML?

You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

How to create a fragment in android java?

To create a blank Fragment , expand app > java in Project: Android view, select the folder containing the Java code for your app, and choose File > New > Fragment > Fragment (Blank).


2 Answers

change:

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

to:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

or---

change:

import android.support.v4.app.Fragment;

to

import android.app.Fragment;
like image 199
vipul mittal Avatar answered Oct 03 '22 09:10

vipul mittal


All the Fragment should be android.app.Fragment. If you want to use android.support.v4.app.Fragment, you should use getSupportFragmentManager() to get the Manager.

like image 42
Andy.Zhao Avatar answered Oct 03 '22 10:10

Andy.Zhao