4

我正在构建一个使用 ActionBarSherlock 的应用程序,它是 ShareActionProvider 用于生成其他应用程序的 Intent。我根据库的 github 页面中的示例代码创建了该项目;起初,我工作完美。然而,在那之后的一天,它就停止了工作。如果我删除有关菜单膨胀的代码,则活动正常加载,否则它会崩溃并出现以下异常:

05-31 22:28:41.200: E/AndroidRuntime(15874): FATAL EXCEPTION: main
05-31 22:28:41.200: E/AndroidRuntime(15874): android.view.InflateException: Binary XML file line #26: Error inflating class <unknown>
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.actionbarsherlock.widget.ActivityChooserView.<init>(ActivityChooserView.java:229)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.actionbarsherlock.widget.ActivityChooserView.<init>(ActivityChooserView.java:202)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.actionbarsherlock.widget.ActivityChooserView.<init>(ActivityChooserView.java:192)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.actionbarsherlock.widget.ShareActionProvider.onCreateActionView(ShareActionProvider.java:164)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.actionbarsherlock.internal.view.ActionProviderWrapper.onCreateActionView(ActionProviderWrapper.java:23)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.ActionProvider.onCreateActionView(ActionProvider.java:98)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.MenuItemImpl.getActionView(MenuItemImpl.java:580)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:235)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:513)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1048)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:91)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:268)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:244)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:946)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:969)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:488)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:819)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3148)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.os.Handler.handleCallback(Handler.java:615)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.os.Looper.loop(Looper.java:137)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at java.lang.reflect.Method.invokeNative(Native Method)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at java.lang.reflect.Method.invoke(Method.java:511)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at dalvik.system.NativeStart.main(Native Method)
05-31 22:28:41.200: E/AndroidRuntime(15874): Caused by: java.lang.reflect.InvocationTargetException
05-31 22:28:41.200: E/AndroidRuntime(15874):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-31 22:28:41.200: E/AndroidRuntime(15874):    ... 33 more
05-31 22:28:41.200: E/AndroidRuntime(15874): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f01000f a=-1}
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.content.res.Resources.loadDrawable(Resources.java:1925)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.View.<init>(View.java:3436)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.view.ViewGroup.<init>(ViewGroup.java:432)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.widget.FrameLayout.<init>(FrameLayout.java:101)
05-31 22:28:41.200: E/AndroidRuntime(15874):    at android.widget.FrameLayout.<init>(FrameLayout.java:97)
05-31 22:28:41.200: E/AndroidRuntime(15874):    ... 36 more

如您所见,没有任何东西指向我的代码;我将问题追溯到此方法的第一行:

public class NewsDetailActivity extends SherlockFragmentActivity{
   .
   .
   .
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getSupportMenuInflater().inflate(R.menu.news_detail, menu);
       //mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
       //mShareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
       return super.onCreateOptionsMenu(menu);
   }

如果删除该行,则该活动就可以正常工作。菜单布局如下图所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
    android:id="@+id/share"
    android:title="@string/share"
    android:showAsAction="ifRoom"
    android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"/>
</menu>

我一直无法解决这个问题,也找不到有类似问题的人。但是,我确实在图书馆的 wiki 中读到主题可能是问题,以下是我的应用程序和操作栏的主题,我已将其更改为 Theme.Sherlock.Light 和许多其他无济于事。

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="Theme.Sherlock">
    <item name="actionBarStyle">@style/ActionBar</item>
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="@style/Widget.Sherlock.ActionBar">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>>
    <item name="android:background">@drawable/background_actionbar</item>
    <item name="android:backgroundStacked">@drawable/background_actionbar</item>
    <item name="android:backgroundSplit">@drawable/background_actionbar</item>
</style>
</resources>

我正在使用最新版本的库。我希望有人可以帮助我!谢谢!

编辑:我无法解决问题,但我在 ActionBarSherlock (layout/abs_activity_chooser_view.xml) 中将异常跟踪到此 XML:

<com.actionbarsherlock.internal.widget.IcsLinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/abs__activity_chooser_view_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    style="?attr/activityChooserViewStyle">

    <FrameLayout
        android:id="@+id/abs__expand_activities_button"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:focusable="true"
        android:addStatesFromChildren="true"
        android:background="?attr/actionBarItemBackground">

        <ImageView android:id="@+id/abs__image"
            android:layout_width="56dip"
            android:layout_height="36dip"
            android:layout_gravity="center"
            android:paddingTop="2dip"
            android:paddingBottom="2dip"
            android:paddingLeft="12dip"
            android:paddingRight="12dip"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/abs__default_activity_button"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:focusable="true"
        android:addStatesFromChildren="true"
        android:background="?attr/actionBarItemBackground">

        <ImageView android:id="@+id/abs__image"
            android:layout_width="56dip"
            android:layout_height="36dip"
            android:layout_gravity="center"
            android:paddingTop="2dip"
            android:paddingBottom="2dip"
            android:paddingLeft="12dip"
            android:paddingRight="12dip"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

    </FrameLayout>

</com.actionbarsherlock.internal.widget.IcsLinearLayout>

似乎异常是由 FrameLayout 引起的。

EDIT 2: Ok, I have found a way to stop the app from crashing. I removed the custom theme from my app and the style from the actionbar, leaving the app theme like:

<application android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock" >

This works well, however, I have lost the possibility of customizing the actionbar.

4

4 回答 4

2

在 AndroidManifest.xml 中将 Activity 的样式更改为 android:theme="@style/Theme.Sherlock"

像这样的东西

    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.Sherlock">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
于 2013-08-21T08:04:12.537 回答
1

如果您在 onCreate 中设置主题,您将能够使用自定义样式而不会出现错误或崩溃。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.YourCustomTheme);
...}

不觉得它是问题的根源,但至少给了你一些选择。

于 2013-06-20T16:57:06.050 回答
1

我遇到了同样的问题,但我是从代码中创建 ShareActionProvider 的。在构造函数中使用getSupportActionBar().getThemedContext()第一个参数ShareActionProvider's解决了这个问题。

于 2014-02-10T17:05:38.730 回答
0

我猜这是一个主题问题。尝试改变:

<style name="ActionBar" parent="@style/Widget.Sherlock.ActionBar">

<style name="ActionBar" parent="Widget.Sherlock.ActionBar.Solid">
于 2013-07-15T22:55:06.820 回答