user3081695
user3081695

Reputation: 55

Navigation drawer Error in replace method while replac ing fragments in onclick event

I am trying to implement navigtion drawer this is my code and i am getting error while running the application.

this is the error in problems of eclipse.

Description Resource Path Location Type The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment) MainActivity.java /NavigationDrawer/src/com/example/navigationdrawer line 198 Java Problem.

This is my code of main activity

    package com.example.testnav;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.content.res.Configuration;
    import android.support.v4.app.ActionBarDrawerToggle;

    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

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

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        // 2. App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // 2.1 create ActionBarDrawerToggle
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_launcher,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );

        // 2.2 Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        // 2.3 enable and show "up" arrow
        getActionBar().setDisplayHomeAsUpEnabled(true); 

        // just styling option
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

         // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
        // then it has handled the app icon touch event

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }





    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
           // Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();


            displayView(position);
            drawerLayout.closeDrawer(drawerListView);

        }
    }


    private void displayView(int position) {    

      Fragment fragment = null;
      switch (position) {
      case 0:
          fragment = new FragmentOne();
          break;
      case 1:
          fragment = new FragmentTwo();
          break;
      case 2:
          fragment = new FragmentThree();
          break;
      case 3:
          fragment = new FragmentOne();
          break;
      case 4:
          fragment = new FragmentOne();
          break;
      case 5:
          fragment = new FragmentOne();
          break;

      default:
          break;
      }

      if (fragment != null) {
          FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.beginTransaction()
                  .replace(R.id.content_frame, fragment).commit();

          // update selected item and title, then close the drawer
          drawerListView.setItemChecked(position, true);
          drawerListView.setSelection(position);

            //setTitle((TextView)view).getText());
          drawerLayout.closeDrawer(drawerListView);
      } else {
          // error in creating fragment
          Log.e("MainActivity", "Error in creating fragment");
      }
    }
}

and this is fragment

    package com.example.testnav;

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

    public class FragmentOne extends Fragment {

    public FragmentOne(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_one, container, false);

        return rootView;
    }
    }

this is my xml

   <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"

        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />

    </android.support.v4.widget.DrawerLayout>

Upvotes: 0

Views: 1250

Answers (2)

Ethan
Ethan

Reputation: 365

I get the same error. Need to modify tow places, as follows.

First, use FragmentActivity

public class MainActivity extends FragmentActivity

Second, change getFragmentManager to getSupportFragmentManager:

FragmentManager fragmentManager = getSupportFragmentManager();

Upvotes: 0

keshav
keshav

Reputation: 3255

Try using FragmentActivity in place of Activity

public class MainActivity extends FragmentActivity

and

 default:
     fragment = new FragmentOne();
     break;

Upvotes: 1

Related Questions