如何更换去precated android.support.v4.app.ActionBarDrawerToggle [英] How to replace deprecated android.support.v4.app.ActionBarDrawerToggle

查看:1287
本文介绍了如何更换去precated android.support.v4.app.ActionBarDrawerToggle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天(17-10-2014)我有更新的Andr​​oid SDK和支持库-v4.jar 我的应用程序,现在我得到德precation警告有关 ActionBarDrawerToggle ,阅读文档看来,我必须使用 ActionBarDrawerToggle 支持库-v7.appcompact.jar

Yesterday (17-10-2014) I have update Android SDK and support-library-v4.jar of my App, now I get deprecation warning related to ActionBarDrawerToggle, reading the documentation seems that I have to use the ActionBarDrawerToggle in support-library-v7.appcompact.jar.

下面我活动的某些部分,可能是相关者:

Here some parts of my Activity that could be relevants:

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

}

我试图复制支持库-V7和替换

I have tried to copy support-library-v7 and 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_drawer

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

这编译,但坠毁在运行时用

this compiles but crash at Runtime with

 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的支持-V7-appcompat.jar 已正确项目依赖性增加

Note that android-support-v7-appcompat.jar is correctly added in project dependencies

推荐答案

只添加 Android的支持-V7-appcompat.jar 来库的依赖是不够的,你必须同时在项目导入,你可以找到在你的SDK的路径 \ Android的SDK \演员\机器人\ SUPPORT \ V7 \ appcompat ,之后添加模块,该模块依赖这种方式配置项目结构

Adding only android-support-v7-appcompat.jar to library dependencies is not enough, you have also to import in your project the module that you can find in your SDK at the path \android-sdk\extras\android\support\v7\appcompatand after that add module dependencies configuring the project structure in this way

否则,只包含支持库的类文件和应用程序无法加载其他资源造成的错误。

otherwise are included only the class files of support library and the app is not able to load the other resources causing the error.

在另外的反向建议替换此

In addition as reVerse suggested replace this

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

这篇关于如何更换去precated android.support.v4.app.ActionBarDrawerToggle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆