0

I am developing an app with navigation drawer and fragments but I can't seem to resolve one error.

I have a error in FragmentTransaction method replace(int,Fragment) . It is showing The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment).

MyFragment is a custom fragment.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity implements OnItemClickListener {

    private DrawerLayout drawerlayout;
    private ListView listview;
    private String[] planets;
    private ActionBarDrawerToggle drawerlistener;
    private ActionBar actionBar;    
    private FragmentTransaction fragmenttransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        listview = (ListView) findViewById(R.id.list);
        planets = getResources().getStringArray(R.array.planets);
        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, planets));
        listview.setOnItemClickListener(this);
        drawerlistener = new ActionBarDrawerToggle(this, drawerlayout, R.drawable.ic_drawer, R.string.drawer_open);

        drawerlayout.setDrawerListener(drawerlistener);
        actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        //getSupportActionBar().setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

        FragmentManager fragmentmanager = getSupportFragmentManager();
        fragmenttransaction = fragmentmanager.beginTransaction();
        loadSelection(0);
    }

    private void loadSelection(int i) {
        // TODO Auto-generated method stub
        listview.setItemChecked(i, true);
        if(i==1)
        {
            MyFragment fragment1 = new MyFragment();
            fragmenttransaction.replace(R.id.main, fragment1);
            fragmenttransaction.commit();
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerlistener.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (drawerlistener.onOptionsItemSelected(item)) {
              return true;
            }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub

        switch (position) {
        case 0:

            break;

        case 1:

            break;

        case 3:

            break;
        default:
            break;
        }
        drawerlayout.closeDrawer(listview);
    }
}

And the MyFragment.java is here:

package com.example.test3;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment
 {
   public MyFragment(){

    }
    @SuppressWarnings({ "deprecation" })
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment1, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}
1
  • 1
    try with Fragment myFragment = new MyFragment() Commented Dec 29, 2015 at 10:28

2 Answers 2

4

Your custom fragment should extend Fragment from support v4 library.

import android.support.v4.app.Fragment, instead of import android.app.Fragment

Sign up to request clarification or add additional context in comments.

1 Comment

I have read your answer and it is good, but should extend import doesn't make any sense, so I decided to edit your answer, so now it is clear to understand.
0

In MyFragment class

import android.support.v4.app.Fragment 

instead of

import android.app.Fragment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.