13

Below is the code of my Activity . In this I am using the support library appcompat

   import android.content.Intent;
   import android.os.Bundle;
   import  android.support.v4.view.MenuItemCompat;
   import android.support.v7.app.ActionBarActivity;
   import android.support.v7.widget.ShareActionProvider;
   import android.view.Menu;
   import android.view.MenuItem;

  public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    MenuItem item = (MenuItem) menu.findItem(R.id.share_options);
    ShareActionProvider shareAction = (ShareActionProvider) MenuItemCompat
            .getActionProvider(item);
    Intent shareIntent = new Intent(Intent.ACTION_SEND)
            .setAction(Intent.ACTION_SEND)
            .putExtra(Intent.EXTRA_TEXT, "MobiTexter")
            .setType("text/plain");
    shareAction.setShareIntent(shareIntent);
    return super.onCreateOptionsMenu(menu);
}

}

The following is my XML file for the menu options that is an ShareActionProvider Widget

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:mobitexter="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/share_options"
    android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    android:orderInCategory="100"
    mobitexter:showAsAction="never"
    mobitexter:title="@string/share_options"/>

  </menu>

In this i am getting an NullPointerException . Below is my log cat output.

          10-02 12:49:00.813: E/AndroidRuntime(9227): FATAL EXCEPTION: main
          10-02 12:49:00.813: E/AndroidRuntime(9227): java.lang.NullPointerException
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at net.mobitexter.easyshare.MainActivity.onCreateOptionsMenu(MainActivity.java:31)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at  android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:392)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:743)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2838)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.os.Handler.handleCallback(Handler.java:605)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.os.Handler.dispatchMessage(Handler.java:92)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.os.Looper.loop(Looper.java:137)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at android.app.ActivityThread.main(ActivityThread.java:4448)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at java.lang.reflect.Method.invokeNative(Native Method)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at java.lang.reflect.Method.invoke(Method.java:511)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
          10-02 12:49:00.813: E/AndroidRuntime(9227):   at dalvik.system.NativeStart.main(Native Method)
Anuranjit Maindola
  • 1,507
  • 1
  • 14
  • 30

3 Answers3

29

I found the error. The problem was that support library requires to have a custom prefix and not android:actionProviderClass . What i was doing wrong that i used android:actionProviderClass instead of customprefix:actionProviderClass

See here: https://developer.android.com/training/basics/actionbar/adding-buttons.html

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Anuranjit Maindola
  • 1,507
  • 1
  • 14
  • 30
  • This would have resolved the NPE, but it still will not make the share action work. How did you resolve the "getActionProvider: item does not implement SupportMenuItem" issue as in http://stackoverflow.com/questions/24219842/getactionprovider-item-does-not-implement-supportmenuitem – faizal Jun 14 '14 at 13:47
2

If this line

shareAction.setShareIntent(shareIntent);

is throwing a NullPointerException, this sentence

(ShareActionProvider) MenuItemCompat.getActionProvider(item);

must be returning null.

Have a look at the post below, which talks about reasons why this would happen.

NullPointerException on setShareIntent using ActionBarSherlock

Community
  • 1
  • 1
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • I am not using actionbarsherlock – Anuranjit Maindola Oct 02 '13 at 07:53
  • Yeah sometimes titles of the questions are misleading, but the point is you may be not correctly setting attribute `android:actionProviderClass` for your item `share_options` – ssantos Oct 02 '13 at 07:57
  • the line used in the `android:actionProviderClass` is copied from what is given in [link]developer.android.com/guide/ui/actionbar – Anuranjit Maindola Oct 02 '13 at 08:19
  • Then chances are that you don't have the class `android.support.v7.widget.ShareActionProvider` due to a different support library. The easiest way to check this, is trying to write that path in a .java source file (using autocompletion). I'm guessing your support library may be `v4` instead of `v7` – ssantos Oct 02 '13 at 08:25
  • Also, according to that link, the right attribute name should be `mobitexter:actionProviderClass` – ssantos Oct 02 '13 at 08:27
0

I changed this

android:actionProviderClass=widget.ShareActionProvider" />

To this

app:actionProviderClass=support.v7.widget.ShareActionProvider" />
Ndheti
  • 266
  • 3
  • 18