尝试ShareActionProvider
从支持库(v7)中使用,如果我不showAsAction
为菜单项设置属性,我的应用程序可以正常工作。我的意图是将此菜单项显示为图标而不是溢出项。
但是,如果我将其设置为始终显示,则应用程序会以NullPointerException
.
07-30 01:23:37.778: E/AndroidRuntime(25853): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v7.internal.widget.ActivityChooserView.updateAppearance(ActivityChooserView.java:510)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v7.internal.widget.ActivityChooserView$3.onChanged(ActivityChooserView.java:247)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v7.internal.widget.ActivityChooserView$ActivityChooserViewAdapter.setDataModel(ActivityChooserView.java:647)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v7.internal.widget.ActivityChooserView.setActivityChooserModel(ActivityChooserView.java:260)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v7.widget.ShareActionProvider.onCreateActionView(ShareActionProvider.java:182)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v4.view.ActionProvider.onCreateActionView(ActionProvider.java:109)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.support.v7.internal.view.menu.MenuItemWrapperJB$ActionProviderWrapperJB.onCreateActionView(MenuItemWrapperJB.java:44)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.MenuItemImpl.getActionView(MenuItemImpl.java:583)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:161)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:438)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1062)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:87)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:216)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:244)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:956)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:979)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:479)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.view.Choreographer.doFrame(Choreographer.java:543)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.os.Handler.handleCallback(Handler.java:733)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.os.Handler.dispatchMessage(Handler.java:95)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.os.Looper.loop(Looper.java:136)
07-30 01:23:37.778: E/AndroidRuntime(25853): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-30 01:23:37.778: E/AndroidRuntime(25853): at java.lang.reflect.Method.invoke(Native Method)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-30 01:23:37.778: E/AndroidRuntime(25853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
我的菜单布局:main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapp.MainActivity" >
<item
android:id="@+id/menu_item_share"
android:title="Share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"/>
</menu>
我在活动中的通货膨胀代码:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
public class MainActivity extends ActionBarActivity
{
...
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
_shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent();
return true;
}
}
谢谢。