Problem while integrating swipe menu in Fragment

  • Replies:5
  • OpenNot stickiedUnanswered
  • Forum posts: 17

Nov 19, 2014 12:23:49 PM via Website

Here i my code for a Fragment menu,But when i inserted swipe menu in this fragment there is a n error displays that "Type mismatch: cannot convert from FindPeopleFragment to Fragment".
Can anybody help me to sort ou the issue.
Here is my codes.

Main activity

import info.androidhive.slidingmenu.adapter.NavDrawerListAdapter;
import info.androidhive.slidingmenu.model.NavDrawerItem;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
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.ListView;

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

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

    mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
    // Pages
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1), true, "50+"));


    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        displayView(0);
    }
}

/**
 * Slide menu item click listener
 * */
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new FindPeopleFragment();

        break;
    case 2:
        fragment = new PhotosFragment();
        break;
    case 3:
        fragment = new CommunityFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        fragment = new WhatsHotFragment();
        break;

    default:
        break;
    }

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

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

}

*FindPeopleFragment *

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi") public class FindPeopleFragment extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_find_people);

        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                      actionBar = getActionBar();
                      actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){
      @Override
      public void onTabReselected(android.app.ActionBar.Tab tab,
          FragmentTransaction ft) {
        // TODO Auto-generated method stub
      }
      @Override
       public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
              Tab.setCurrentItem(tab.getPosition());
          }
      @Override
      public void onTabUnselected(android.app.ActionBar.Tab tab,
          FragmentTransaction ft) {
        // TODO Auto-generated method stub
      }};
      //Add New Tab
      actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
    }
}

— modified on Nov 19, 2014 12:26:34 PM

  • Forum posts: 118

Nov 19, 2014 12:33:06 PM via Website

FindPeopleFragment is not a fragment but a (fragment)activity. You can only use Fragment classes in FragmentManager.

— modified on Nov 19, 2014 12:33:54 PM

  • Forum posts: 17

Nov 19, 2014 12:34:54 PM via Website

So how can i solve the issue?
All i want is to swipe the FindPeopleFragment with 3 sections.

  • Forum posts: 118

Nov 19, 2014 12:44:25 PM via Website

Ashfaq

So how can i solve the issue?
All i want is to swipe the FindPeopleFragment with 3 sections.

http://stackoverflow.com/questions/21364568/android-adding-tab-inside-fragment#answer-21364674 this answer gives you a clearly example of how your FindPeopleFragment should look like. That only if you stil wish to use the navigation drawer in that fragment. Otherwise I recommend to startActivity FindPeopleFragment in your MainActivity instead.

In case it's not necessary to use your navigation drawer, select the following in your MainActivity:

case 1:
        fragment = new FindPeopleFragment();

        break;

Replace it with this:

case 1:
        Intent intent = new Intent(this, FindPeopleFragment.class);
        startActivity(intent);
        break;

— modified on Nov 19, 2014 12:45:00 PM

  • Forum posts: 17

Nov 19, 2014 12:51:01 PM via Website

Replace it with this:

case 1:
Intent intent = new Intent(this, FindPeopleFragment.class);
startActivity(intent);
break;

In the above case the app force closing.
what to do?

here is the error log cat

11-19 11:50:20.803: W/EGL_genymotion(3264): eglSurfaceAttrib not implemented

11-19 11:50:20.803: E/OpenGLRenderer(3264): Getting MAX_TEXTURE_SIZE from GradienCache
11-19 11:50:20.819: E/OpenGLRenderer(3264): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
11-19 11:50:20.819: D/OpenGLRenderer(3264): Enabling debug mode 0
11-19 11:50:23.539: D/AndroidRuntime(3264): Shutting down VM
11-19 11:50:23.539: W/dalvikvm(3264): threadid=1: thread exiting with uncaught exception (group=0xa4baa648)
11-19 11:50:23.567: E/AndroidRuntime(3264): FATAL EXCEPTION: main
11-19 11:50:23.567: E/AndroidRuntime(3264): android.content.ActivityNotFoundException: Unable to find explicit activity class {info.androidhive.slidingmenu/info.androidhive.slidingmenu.FindPeopleFragment}; have you declared this activity in your AndroidManifest.xml?
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.Activity.startActivityForResult(Activity.java:3390)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.Activity.startActivityForResult(Activity.java:3351)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.Activity.startActivity(Activity.java:3587)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.Activity.startActivity(Activity.java:3555)
11-19 11:50:23.567: E/AndroidRuntime(3264): at info.androidhive.slidingmenu.MainActivity.displayView(MainActivity.java:177)
11-19 11:50:23.567: E/AndroidRuntime(3264): at info.androidhive.slidingmenu.MainActivity.access$0(MainActivity.java:168)
11-19 11:50:23.567: E/AndroidRuntime(3264): at info.androidhive.slidingmenu.MainActivity$SlideMenuClickListener.onItemClick(MainActivity.java:129)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.widget.AbsListView$1.run(AbsListView.java:3463)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.os.Handler.handleCallback(Handler.java:730)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.os.Looper.loop(Looper.java:137)
11-19 11:50:23.567: E/AndroidRuntime(3264): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-19 11:50:23.567: E/AndroidRuntime(3264): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 11:50:23.567: E/AndroidRuntime(3264): at java.lang.reflect.Method.invoke(Method.java:525)
11-19 11:50:23.567: E/AndroidRuntime(3264): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-19 11:50:23.567: E/AndroidRuntime(3264): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-19 11:50:23.567: E/AndroidRuntime(3264): at dalvik.system.NativeStart.main(Native Method)
11-19 11:50:25.083: I/Process(3264): Sending signal. PID: 3264 SIG: 9

  • Forum posts: 118

Nov 19, 2014 1:40:10 PM via Website

11-19 11:50:23.567: E/AndroidRuntime(3264): android.content.ActivityNotFoundException: Unable to find explicit activity class {info.androidhive.slidingmenu/info.androidhive.slidingmenu.FindPeopleFragment}; have you declared this activity in your AndroidManifest.xml?

This is what your logcat says, are you sure that your activity is added to your Android Manifest?