具有版本兼容性的Android ActionBar共享

时间:2015-03-15 16:56:32

标签: android shareactionprovider

我想通过Android ShareActionProvider分享。

我的代码是

<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.shreegorji.MyActionBarActivity" >

<!--   android:onClick="onGroupItemClick"

-->
<item android:id="@+id/action_share"
      android:icon="@drawable/ic_action_share"
      android:title="@string/action_share"
      app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
      app:showAsAction="ifRoom"
     />

这是我的java类代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_action_bar, menu);

    this.menu=menu;

    //set icon
    menu.findItem(R.id.submenu_item1).setIcon(null);
    menu.findItem(R.id.submenu_item2).setIcon(null);
    menu.findItem(R.id.submenu_item3).setIcon(null);

    if(setDefault.getInt("Language", 3)==1) menu.findItem(R.id.submenu_item1).setIcon(R.drawable.ic_action_accept);
    else if(setDefault.getInt("Language", 3)==2) menu.findItem(R.id.submenu_item2).setIcon(R.drawable.ic_action_accept);
    else menu.findItem(R.id.submenu_item3).setIcon(R.drawable.ic_action_accept);

    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);
    ShareActionProvider mShareActionProvider = (ShareActionProvider)
            MenuItemCompat.getActionProvider(shareItem);
    mShareActionProvider.setShareIntent(getDefaultIntent());

    return super.onCreateOptionsMenu(menu);
}

private Intent getDefaultIntent() {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    String msg = "Gurudev Says: "+shareText;
    Log.e("msg",msg);
    intent.putExtra(Intent.EXTRA_TEXT,msg);

    return intent;
}

但是当我尝试通过我的应用程序共享时,这会抛出NullPointerException。此代码也无法使用API​​级别&lt; 11。

当我尝试在API级别11下运行此代码时,它会抛出异常。请指导我。

我的例外是

03-15 22:16:20.527: E/AndroidRuntime(472): FATAL EXCEPTION: main
03-15 22:16:20.527: E/AndroidRuntime(472): java.lang.NullPointerException
03-15 22:16:20.527: E/AndroidRuntime(472):  at     android.support.v7.internal.widget.ActivityChooserView.updateAppearance(ActivityChooserView.java:512)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.internal.widget.ActivityChooserView.access$400(ActivityChooserView.java:68)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.internal.widget.ActivityChooserView$3.onChanged(ActivityChooserView.java:249)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.internal.widget.ActivityChooserView$1.onChanged(ActivityChooserView.java:129)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.internal.widget.ActivityChooserModel.ensureConsistentState(ActivityChooserModel.java:663)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.internal.widget.ActivityChooserModel.setIntent(ActivityChooserModel.java:376)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.widget.ShareActionProvider.setShareIntent(ShareActionProvider.java:304)
03-15 22:16:20.527: E/AndroidRuntime(472):  at com.example.shreegorji.ThoughtsContainerActivity.onCreateOptionsMenu(ThoughtsContainerActivity.java:94)
03-15 22:16:20.527: E/AndroidRuntime(472):  at  android.app.Activity.onCreatePanelMenu(Activity.java:2148)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.os.Handler.handleCallback(Handler.java:587)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.os.Looper.loop(Looper.java:123)
03-15 22:16:20.527: E/AndroidRuntime(472):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-15 22:16:20.527: E/AndroidRuntime(472):  at java.lang.reflect.Method.invokeNative(Native Method)
03-15 22:16:20.527: E/AndroidRuntime(472):  at java.lang.reflect.Method.invoke(Method.java:521)
03-15 22:16:20.527: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 03-15 22:16:20.527: E/AndroidRuntime(472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 03-15 22:16:20.527: E/AndroidRuntime(472):     at dalvik.system.NativeStart.main(Native Method)
 03-15 22:45:19.917: E/Null(502): java.lang.NullPointerException
 03-15 22:45:20.417: E/msg(502): Gurudev Says: 
 03-15 22:45:20.507: E/AndroidRuntime(502): FATAL EXCEPTION: main
 03-15 22:45:20.507: E/AndroidRuntime(502): java.lang.NullPointerExceptio

1 个答案:

答案 0 :(得分:0)

很难说,因为你只发布了这个方法,但我将会这样做:why MenuItemCompat.getActionProvider returns null?