java.lang.NoClassDefFoundError:调用片段时

时间:2015-06-11 23:21:51

标签: java android android-fragments noclassdeffounderror

我正在尝试使用SherlockFragment创建一个片段,从导航抽屉活动中选择以显示选项卡式片段。我的主要活动如下:

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();
}
}

大多数片段都是这样构建的

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;
}
}

选择菜单项时出现问题,我收到以下错误

java.lang.NoClassDefFoundError: com.main.impact.HomeFragment

这对我来说有点新鲜,所以我不确定还有什么要包含,不用说我会更新问题,如果需要的话。提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,非常感谢帮助我解决这个问题的所有人。我设法将此错误排序。

问题在于使用SherlockFragment。我只是更改了我的片段以扩展android.support.v4.app.Fragment并在我的主要活动中对displayView进行了以下更改

if (fragment != null) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_container, fragment);
        fragmentTransaction.commit();
        ...