2014-03-13 80 views
1

我正在使用滑動菜單,但它僅適用於設備高於3.0的設備。 我想使用不同版本的android運行相同的代碼。任何人都可以幫助我找到解決方案?在android中的滑動菜單

這裏是我的代碼

public class WelcomeActivity extends Activity { 
private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private ActionBarDrawerToggle mDrawerToggle; 
private CharSequence mDrawerTitle; 
private CharSequence mTitle; 
private String[] navMenuTitles; 
private TypedArray navMenuIcons; 
private ArrayList<NavDrawerItem> navDrawerItems; 
private NavDrawerListAdapter adapter; 
@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 
    mTitle = mDrawerTitle = getTitle(); 
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 
    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>(); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0],navMenuIcons.getResourceId(0, -1))); 
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); 
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 
navMenuIcons.recycle(); 
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 
    adapter = new NavDrawerListAdapter(getApplicationContext(),navDrawerItems); 
    mDrawerList.setAdapter(adapter); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, 
      R.string.app_name, 
      R.string.app_name 
    ) 
{ 
public void onDrawerClosed(View view) { 
getActionBar().setTitle(mTitle); 
invalidateOptionsMenu(); 
} 
public void onDrawerOpened(View drawerView) { 
getActionBar().setTitle(mDrawerTitle); 
invalidateOptionsMenu(); 
} 
}; 
mDrawerLayout.setDrawerListener(mDrawerToggle); 
if (savedInstanceState == null) { 
displayView(0); 
    } 
} 

private class SlideMenuClickListener implements 
    ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{ 
displayView(position);} 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    menu.getItem(0).getSubMenu(); 
    return true; 
} 




private void displayView(int position) { 

    Fragment fragment = null; 
    switch (position) { 
    case 0: 
     fragment = new HomeFragment(); 
     break; 
    case 1: 
     fragment = BackupFragment.getInstance(); 
     break; 
    case 2: 
     fragment = new RestoreFragment(); 
     break; 
    default: 
     break; 
    } 
    if (fragment != null) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.frame_container, fragment).commit(); 
     mDrawerList.setItemChecked(position, true); 
     mDrawerList.setSelection(position); 
     setTitle(navMenuTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } else { 
     //erreur de creation de fraglent 
     Log.e("MainActivity", "Error in creating fragment"); 
    } 
} 
@Override 
public void setTitle(CharSequence title) { 
    mTitle = title; 
    getActionBar().setTitle(mTitle); 
} 
@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); 
} 
@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

}

+0

也許某些功能只針對特定版本的Android啓用,但這只是一種猜測,因爲您沒有提供示例。請分享一些代碼。 –

+0

在這裏粘貼你的代碼 – Dev

+0

我編輯了我的帖子,現在你可以現在我的代碼。它在3.0 device'version中運行良好,但當我更改爲較低版本的設備時,它會發生! – Marya

回答

0

你的應用程序崩潰,因爲ActionBar沒有在Honeycomb(3.0)下面支持。

要避免此問題,您必須使用support library

爲了有一個更好的解釋:

The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above. 

**Caution:** Be certain you import the ActionBar class (and related APIs) from the appropriate package: 
  • 如果支持API級別低於11:

    import android.support.v7.app.ActionBar

  • 如果只支持API級別11及更高版本:

    import android.app.ActionBar

希望它有幫助!

+0

是的,它是它在Android 3.0中添加的問題。 現在當我添加「import android.support.v7.app.ActionBar」 我得到這個錯誤: 導入android.support.v7無法解析 – Marya

+0

首先,你必須設置支持庫(看一看在我的答案鏈接);然後你必須instanciate'ActionBar'調用'getSupportActionBar()'而不是'getActionBar()' – yugidroid

+0

我的代碼不支持「getSupportActionBar()」我得到一個錯誤! – Marya

0

使用MenuDrawer庫。很棒!

+0

我已經在使用它 – Marya

+0

然後它不應該是一個問題,MenuDrawer在API 7和以上工作。 –