2015-06-11 66 views
0

我試圖使用SherlockFragment創建一個片段,從導航抽屜活動中選擇以顯示選項卡片段。我的主要活動如下:java.lang.NoClassDefFoundError:在調用片段

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 

這對我來說有點新,所以我不確定究竟還有什麼其他的東西可以包括在內,不用說我會更新應該要求的問題。提前致謝。

+0

的代碼沒有問題做了如下改變displayView。嘗試重新打開你的IDE,做一個重建,確保沒有生成錯誤。 –

+0

不是你的答案,但看看這篇文章:[爲什麼我得到一個NoClassFoundError在Java](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-在-JAVA)。希望它能指出你正確的方向。 – JNYRanger

+0

嘗試在'fragmentTransaction.replace()'方法中傳遞'fragment'對象而不是'sherfragment'。 – SAM

回答

0

首先非常感謝所有幫助過我的人。我設法得到這個錯誤排序。

問題在於使用SherlockFragment。我只是改變了我的片段,延長android.support.v4.app.Fragment,在我的主要活動

if (fragment != null) { 

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