我已经被困在这个问题上几天了,我正在失去我的头发。我在网上搜索了一页问题,但没有找到答案,所以希望得到一些帮助!我有一个Android 4.0应用程序,主要活动有一个导航抽屉 - 它工作正常。根据所选的菜单项,我加载Fragment或ListFragment。再次这很好。
现在,我正在尝试使用ActionbarSherlock为Gingerbread提供支持。我更新了我的主要活动和使用ABS库的代码,但我现在只在我的2.3版仿真器上出现错误!更新后的代码仍可在4.0 +上正常使用!
我收到以下错误:二进制XML文件行#18:在我的activity_main.xml上输出类错误
也许我一直盯着这个太长时间,但我无法理解为什么它适用于ICS而不是姜饼!有谁知道我在这里缺少什么?
这是我的代码:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
strings.xml片段:
<!-- Nav Drawer Menu Items -->
<string-array name="nav_drawer_items">
<item>Frag 1</item>
<item>Frag 2</item>
<item>Frag 3</item>
<item>Frag 4</item>
</string-array>
<!-- Nav Drawer List Item Icons -->
<!-- Keep them in order as the titles are in -->
<array name="nav_drawer_icons">
<item>@drawable/ic_nav_drawer_frag1</item>
<item>@drawable/ic_nav_drawer_frag2</item>
<item>@drawable/ic_nav_drawer_frag3</item>
<item>@drawable/ic_nav_drawer_frag4</item>
</array>
这是我的MainActivity.java:
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class MainActivity extends SherlockFragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Fragment 1
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
// Fragment 2
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
// Fragment 3
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
// Fragment 4
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, // nav menu toggle icon
R.string.app_name, // nav drawer open - description for
// accessibility
R.string.app_name // nav drawer close - description for
// accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for item list
// displayView(startingFragment);
displayView(startingFragment);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setVisible(!drawerOpen);
}
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
Intent intent;
// update the main content by replacing fragments
SherlockFragment fragment = null;
SherlockListFragment listFragment = null;
Bundle bundle = null;
switch (position) {
case 0: // Fragment 1
listFragment = new Fragment1();
break;
case 1: // Fragment 2
listFragment = new Fragment2();
break;
case 2: // Fragment 3
listFragment = new Fragment3();
break;
case 3: // Fragment 4
fragment = new Fragment4();
break;
default:
break;
}
if ((fragment != null) || (listFragment != null)) {
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
} else if (listFragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, listFragment).commit();
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
}
}
@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);
}
}
我还更新了清单以使用android:minSdkVersion =“8”和android:theme =“@ style / Theme.Sherlock.Light.DarkActionBar”
堆栈追踪:
04-26 19:18:26.092: E/AndroidRuntime(446): FATAL EXCEPTION: main
04-26 19:18:26.092: E/AndroidRuntime(446): android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
04-26 19:18:26.092: E/AndroidRuntime(446): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-26 19:18:26.092: E/AndroidRuntime(446): at ca.brainservice.pricecruncher.free.util.NavDrawerListAdapter.getView(NavDrawerListAdapter.java:47)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.ListView.makeAndAddView(ListView.java:1745)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.ListView.fillSpecific(ListView.java:1290)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.ListView.layoutChildren(ListView.java:1588)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.layout(View.java:7175)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.layout(View.java:7175)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.layout(View.java:7175)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.layout(View.java:7175)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.layout(View.java:7175)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.layout(View.java:7175)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.os.Looper.loop(Looper.java:123)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-26 19:18:26.092: E/AndroidRuntime(446): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 19:18:26.092: E/AndroidRuntime(446): at java.lang.reflect.Method.invoke(Method.java:507)
04-26 19:18:26.092: E/AndroidRuntime(446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 19:18:26.092: E/AndroidRuntime(446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 19:18:26.092: E/AndroidRuntime(446): at dalvik.system.NativeStart.main(Native Method)
04-26 19:18:26.092: E/AndroidRuntime(446): Caused by: java.lang.reflect.InvocationTargetException
04-26 19:18:26.092: E/AndroidRuntime(446): at java.lang.reflect.Constructor.constructNative(Native Method)
04-26 19:18:26.092: E/AndroidRuntime(446): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
04-26 19:18:26.092: E/AndroidRuntime(446): ... 35 more
04-26 19:18:26.092: E/AndroidRuntime(446): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
04-26 19:18:26.092: E/AndroidRuntime(446): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.view.View.<init>(View.java:2108)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.TextView.<init>(TextView.java:344)
04-26 19:18:26.092: E/AndroidRuntime(446): at android.widget.TextView.<init>(TextView.java:337)
04-26 19:18:26.092: E/AndroidRuntime(446): ... 38 more
答案 0 :(得分:0)
我暂时无法发表评论,但我注意到您使用的是invalidateOptionsMenu()
而不是supportInvalidateOptionsMenu()
。我不确定这是否能解决您的问题,但这看起来不对。