昨天(2014年10月17日),我已经更新了Android SDK和support-library-v4.jar
我的App,现在我收到了有关弃用警告ActionBarDrawerToggle
,阅读文档似乎必须使用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
已正确添加到项目依赖项中