如何replace弃用的android.support.v4.app.ActionBarDrawerToggle

昨天(17-10-2014)我已经更新了我的应用程序的Android SDK和support-library-v4.jar ,现在我得到了有关ActionBarDrawerToggle弃用警告,阅读文档似乎我必须在support-library-v7.appcompact.jar使用ActionBarDrawerTogglesupport-library-v7.appcompact.jar

在这里,我的活动的一些部分可能是相关的:

 import android.app.ActionBar; import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.webkit.WebView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; public class MyActivity extends FragmentActivity { private ActionBar bar; private CustomActionBarDrawerToggle mDrawerToggle; private DrawerLayout mDrawer; private ListView mDrawerList; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_infoviewer); bar = this.getActionBar(); bar.setDisplayHomeAsUpEnabled(true); bar.setHomeButtonEnabled(true); bar.setDisplayShowTitleEnabled(false); mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawer.setBackgroundColor(getResources().getColor(R.color.White)); initNavMenu(); try { mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer); } catch (RuntimeException e) { e.printStackTrace(); } mDrawer.setDrawerListener(mDrawerToggle); } .... private void initNavMenu() { NavMenuAdapter mAdapter = MyDrawers.getDefaultDrawer(MyActivity.this, true); mDrawerList = (ListView) findViewById(R.id.drawer); mDrawerList.setBackgroundColor(getResources().getColor(R.color.GreenMoneyDark)); if (mDrawerList != null) mDrawerList.setAdapter(mAdapter); mDrawerList.setOnItemClickListener(new DrawerItemClickListener(MyActivity.this, mDrawer, mDrawerList)); } @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); mDrawerToggle.onConfigurationChanged(newConfig); } private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle { public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout) { super(mActivity, mDrawerLayout, R.drawable.action_drawer, R.string.ns_menu_open, R.string.ns_menu_close); } @Override public void onDrawerClosed(View view) { bar.setTitle(getString(R.string.ns_menu_close)); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } @Override public void onDrawerOpened(View drawerView) { bar.setTitle(getString(R.string.ns_menu_open)); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } } } 

我试图复制support-library-v7并replace

 import android.support.v4.app.ActionBarDrawerToggle; 

  import android.support.v7.app.ActionBarDrawerToggle; 

这导致了编译问题

  public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout) { super(mActivity, mDrawerLayout, R.drawable.action_drawer, R.string.ns_menu_open, R.string.ns_menu_close); } 

所以我试图用R.drawable.action_drawerreplace

 public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout) { super(mActivity, mDrawerLayout,new Toolbar(MyActivity.this) , R.string.ns_menu_open, R.string.ns_menu_close); } 

这编译但在运行时崩溃

  java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$attr; at android.support.v7.widget.Toolbar.<init>(Toolbar.java:190) at android.support.v7.widget.Toolbar.<init>(Toolbar.java:186) 

请注意, android-support-v7-appcompat.jar已正确添加到项目依赖项中 在这里输入图像描述

只添加android-support-v7-appcompat.jar到库依赖是不够的,你还需要在你的项目中导入你可以在你的SDK中find的path\android-sdk\extras\android\support\v7\appcompat然后在这之后添加configuration项目结构的模块依赖项

在这里输入图像描述

否则只包含支持库的类文件,应用程序不能加载导致错误的其他资源。

另外如reVersebuild议replace这个

 public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout) { super(mActivity, mDrawerLayout,new Toolbar(MyActivity.this) , R.string.ns_menu_open, R.string.ns_menu_close); } 

 public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout) { super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close); } 

没有必要使用需要工具栏的ActionBarDrawerToggle超级调用。 这意味着不使用以下构造函数:

 ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) 

你应该使用这个:

 ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) 

所以基本上你唯一需要做的就是删除你的自定义drawable

 super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close); 

更多关于文档中的“新”ActionBarDrawerToggle (点击) 。

你必须使用import android.support.v7.app.ActionBarDrawerToggle;

并使用构造函数

 public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout) { super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close); } 

如果抽屉切换button变暗,则必须使用支持库中提供的supportActionBar。

你可以从这个链接实现supportActionbar: http : //developer.android.com/training/basics/actionbar/setting-up.html

绝望的

 drawer.setDrawerListener(toggle); 

您可以使用

 drawer.addDrawerListener(toggle);