我正在使用支持库中的 DrawerLayout,据说库中的这些 API 适用于 Android API 4+
出于某种原因,我无法让它在 API 8 上运行(不确定其他)在 Android 4.0++ 上运行良好
任何 Android API 8 都不起作用(在我的许多朋友的手机上测试过)。我是否对 DrawerLayout 做错了什么不能使其与 API8 一起使用?
LOGCAT
08-30 02:50:36.753: E/AndroidRuntime(450): FATAL EXCEPTION: main
08-30 02:50:36.753: E/AndroidRuntime(450): android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
08-30 02:50:36.753: E/AndroidRuntime(450): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.ListView.makeAndAddView(ListView.java:1727)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.ListView.fillDown(ListView.java:652)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.ListView.fillFromTop(ListView.java:709)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.ListView.layoutChildren(ListView.java:1580)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.View.layout(View.java:7035)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.View.layout(View.java:7035)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.View.layout(View.java:7035)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.View.layout(View.java:7035)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.os.Looper.loop(Looper.java:123)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 02:50:36.753: E/AndroidRuntime(450): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 02:50:36.753: E/AndroidRuntime(450): at java.lang.reflect.Method.invoke(Method.java:521)
08-30 02:50:36.753: E/AndroidRuntime(450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 02:50:36.753: E/AndroidRuntime(450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 02:50:36.753: E/AndroidRuntime(450): at dalvik.system.NativeStart.main(Native Method)
08-30 02:50:36.753: E/AndroidRuntime(450): Caused by: java.lang.reflect.InvocationTargetException
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.TextView.<init>(TextView.java:321)
08-30 02:50:36.753: E/AndroidRuntime(450): at java.lang.reflect.Constructor.constructNative(Native Method)
08-30 02:50:36.753: E/AndroidRuntime(450): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
08-30 02:50:36.753: E/AndroidRuntime(450): ... 29 more
08-30 02:50:36.753: E/AndroidRuntime(450): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/list_selector_background.xml from drawable resource ID #0x0
08-30 02:50:36.753: E/AndroidRuntime(450): at android.content.res.Resources.loadDrawable(Resources.java:1697)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.view.View.<init>(View.java:1885)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.widget.TextView.<init>(TextView.java:327)
08-30 02:50:36.753: E/AndroidRuntime(450): ... 33 more
08-30 02:50:36.753: E/AndroidRuntime(450): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/list_selector_background.xml from xml type drawable resource ID #0x0
08-30 02:50:36.753: E/AndroidRuntime(450): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1924)
08-30 02:50:36.753: E/AndroidRuntime(450): at android.content.res.Resources.loadDrawable(Resources.java:1692)
08-30 02:50:36.753: E/AndroidRuntime(450): ... 36 more
XML 文件
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
主班
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
Settings home = new Settings();
if (position == 0) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, home).commit();
} else if (position == 1) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, home).commit();
} else if (position == 2) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, home).commit();
}
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@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);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}