-6

I going to add a fragment to an Activity . But its shows following problems . May be it's not compatible .Has there any solution ?

    media/arifhasnat/1ED0E5663F78E3C1/
AjkerDeal/CustomNavigation/MyApplication/
app/src/main/java/navigationdrawer/arifhasnat
/com/androidcustomnavigationdrawer/
MainActivity.java:22: error: incompatible types:
 FragmentOne cannot be converted to Fragment
fragmentTransaction.replace(R.id.frame_one, new FragmentOne()).commit();

Here my code: Its the Main Activity where i called Fragment class

   package navigationdrawer.arifhasnat.com.androidcustomnavigationdrawer;

    import android.os.Bundle;

    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ListView;


    public class MainActivity extends AppCompatActivity {

        private String[] mNavigationDrawerItemTitles;
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;

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


            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame_one, new FragmentOne()).commit();


        }

    }

Fragment :

package navigationdrawer.arifhasnat.com.androidcustomnavigationdrawer;

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

/**
 * Created by arifhasnat on 1/5/16.
 */
public class FragmentOne extends Fragment {

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


    }
}
arifhasnat
  • 93
  • 1
  • 2
  • 10
  • bacause`Fragment` class from compat library is not derived from `Fragment` class from android SDK – Selvin Jan 05 '16 at 14:57

3 Answers3

2

Your FragmentOne extends from android.app.Fragment, while your MainActivity use the support library android.support.v4.app.Fragment.

Take a look at Difference between android.app.Fragment and android.support.v4.app.Fragment.

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
2

FragmentOne inherits from android.app.Fragment, apparent from the import declaration at the top of your class file:

import android.app.Fragment;

Conversely, your FragmentTransaction is from the support library:

import android.support.v4.app.FragmentTransaction;

The two types are incompatible. To resolve the issue, change the import declaration for FragmentOne so that it reads import android.support.v4.app.Fragment;

PPartisan
  • 8,173
  • 4
  • 29
  • 48
0

So Here the answer.

i have replace

import android.app.Fragment;

with

 import android.support.v4.app.Fragment;

In the Class: where i have imported an wrong package library

package navigationdrawer.arifhasnat.com.androidcustomnavigationdrawer;

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

/**
 * Created by arifhasnat on 1/5/16.
 */
public class FragmentOne extends Fragment {

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


    }
}
arifhasnat
  • 93
  • 1
  • 2
  • 10