I am trying to use SherlockFragment to create a Fragment, selected from a navigation drawer activity to show a tabbed fragment. My main activity is below:
package com.impact.main;
import com.impact.main.R;
import com.impact.adapters.NavDrawerListAdapter;
import com.impact.helper.SQLiteHandler;
import com.impact.helper.SessionManager;
import com.impact.helper.dbTables;
import com.impact.model.NavDrawerItem;
import java.util.ArrayList;
import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.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;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
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;
private com.impact.helper.SQLiteHandler db;
private com.impact.helper.SessionManager session;
GoogleCloudMessaging gcm;
Context context;
String[] title;
Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// SqLite database handler
db = new com.impact.helper.SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
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>();
//get length of menu list
String storedStaffId = SQLiteHandler.getUser(getApplicationContext(), "staffid");
int length = getResources().getStringArray(R.array.nav_drawer_items).length;
int projectsCount = db.getRowCount(dbTables.TABLE_PROJECTS,
"INNER JOIN projectstaff AS p ON p.projectid=projects.projectid WHERE staffid=" + storedStaffId);
// adding nav drawer items to array
for(int i=0; i<length; i++){
if(i == 1 ){
//add menu item with user project count
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1), true, ""+projectsCount));
}else{
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1)));
}
}
// 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
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("");
// 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.logout:
logoutUser();
return true;
case R.id.unlink:
Toast.makeText(getApplicationContext(), "No Data Connection \nOperating in offline mode", Toast.LENGTH_SHORT).show();
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);
boolean isOnline = SessionManager.isOnline(getApplicationContext());
if(isOnline == false){
menu.findItem(R.id.unlink).setVisible(true);
}else{
menu.findItem(R.id.unlink).setVisible(false);
}
menu.findItem(R.id.logout).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
SherlockFragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new ProjectsFragment();
break;
case 2:
fragment = new WorkListFragment();
break;
case 3:
fragment = new ChangeControlFragment();
break;
case 4:
fragment = new CostControlFragment();
break;
case 5:
fragment = new MeetingsFragment();
break;
case 6:
fragment = new ActionsFragment();
break;
case 7:
fragment = new PerformanceFragment();
break;
case 8:
fragment = new DocumentsFragment();
break;
case 9:
fragment = new ReportsFragment();
break;
case 10:
fragment = new ProjectAdminFragment();
break;
case 11:
fragment = new SystemAdminFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SherlockFragment sherfragment = new SherlockFragment();
fragmentTransaction.replace(R.id.frame_container, sherfragment);
fragmentTransaction.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);
}
/**
* Logging out the user. Will set isLoggedIn flag to false in shared
* preferences Clears the user data from sqlite users table
* */
public void logoutUser() {
// notifies user
//GCMIntentService.generateNotification(getApplicationContext(), "Logged out");
session.setLogin(false);
db.emptyTable("login");
// Launching the login activity
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
Most of the fragments are constructed like this
package com.impact.main;
import com.actionbarsherlock.app.SherlockFragment;
import com.impact.main.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends SherlockFragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
The problem comes when selecting the menu item, I get the following error
java.lang.NoClassDefFoundError: com.main.impact.HomeFragment
This is a bit new to me so I'm not sure exactly what else to include, needless to say that i'll update the question should that be required. Thanks in advance.
First of all thanks a lot to everyone who helped me out with this. I've managed to get this error sorted.
The problem was with using SherlockFragment. I simply changed my fragments to extend
android.support.v4.app.Fragment
and made the following change todisplayView
in my main activity