如何替换弃用的android.support.v4.app.ActionBarDrawerToggle

时间:2014-10-18 12:15:20

标签: android android-support-library

昨天(2014年10月17日)我已更新Android SDK和support-library-v4.jar我的应用,现在我收到与ActionBarDrawerToggle相关的弃用警告,阅读the documentation似乎我必须使用ActionBarDrawerToggle中的support-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并替换

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_drawer
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已在项目依赖项中正确添加 enter image description here

4 个答案:

答案 0 :(得分:66)

仅向库依赖项添加android-support-v7-appcompat.jar是不够的,您还必须在项目中导入可在路径\android-sdk\extras\android\support\v7\appcompat中的SDK中找到的模块,然后添加模块依赖项以配置这样的项目结构

enter image description here

否则仅包含支持库的类文件,并且应用程序无法加载导致错误的其他资源。

此外,reVerse建议更换此

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

答案 1 :(得分:60)

您无需使用需要ToolbarActionBarDrawerToggle超级电话。这意味着不使用以下构造函数:

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 in the Docs (click)的更多信息。

答案 2 :(得分:27)

您必须使用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);
}

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

您可以通过以下链接实施supportActionbar:http://developer.android.com/training/basics/actionbar/setting-up.html

答案 3 :(得分:6)

Insted of

drawer.setDrawerListener(toggle);

您可以使用

drawer.addDrawerListener(toggle);