0

I have tried to implement a share button in an android app using the v7.support libraries and not using them. I am trying to use this code, but it throws NPE on getActionProvider:

WebView webs = (WebView)findViewById(R.id.welcomeWebview);
ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this).setType("text/html").setHtmlText(webs.getUrl());


getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = (MenuItem) findViewById(R.id.menu_item_share);
Intent intent = b.getIntent();
MenuItemCompat.getActionProvider(item);
ShareActionProvider mShareActionProvider = (ShareActionProvider)item.getActionProvider();
mShareActionProvider.setShareIntent(intent);

Here is my relevant XML.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.tgwf.www.tgwf" >
<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>


</manifest>

And the LogCat stack is here :

11-02 06:17:50.005 11198-11198/org.tgwf.www.tgwf E/SysUtils: ApplicationContext is null in ApplicationStatus
11-02 06:17:50.063 11198-11198/org.tgwf.www.tgwf E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
11-02 06:17:50.063 11198-11198/org.tgwf.www.tgwf E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
11-02 06:17:50.132 11198-11198/org.tgwf.www.tgwf E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008d57
11-02 06:17:50.199 11198-11198/org.tgwf.www.tgwf E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
samdoj
  • 144
  • 8

3 Answers3

0

item is null here.

Instead of this:

  MenuItem item = (MenuItem) findViewById(R.id.menu_item_share);

use this:

 MenuItem item = (MenuItem) menu.findItem(R.id.menu_item_share);

Also, it's app:actionProviderClass and not android:actionProviderClass.

Henry
  • 17,490
  • 7
  • 63
  • 98
0

I recommed to use the class from the support library:

<item
    android:id="@+id/menu_item_share"
    android:title="Share"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    app:showAsAction="ifRoom"/>

very important your Activity has to extends ActionBarActivity:

MyActivity extends ActionBarActivity

the correct imports:

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

Use this as you shareProvider layout..

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_share"
        android:title="Share"
        app:showAsAction="always"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        />
</menu>

And then in your activity or in your fragment, override this method..

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.member_fragment,menu);
        MenuItem menuItem = menu.findItem(R.id.action_share);
        ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
        if(mShareActionProvider!=null){
            if(memberBean!=null){
                mShareActionProvider.setShareIntent(createShare());
            }
        }
    }
    private Intent createShare() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT,getDataToShare());
        return shareIntent;
    }

Here memberBean is the java bean from which i am getting data to share. You can change it, according to your requirement.

Ritt
  • 3,181
  • 3
  • 22
  • 51
  • For some reason, this gives me a button that does nothing, and displays the text "SHARE" rather than the icon. – samdoj Nov 02 '15 at 20:11
  • okay, is actionbar enabled in your fragment. Try adding setHasOptionsMenu(true); in oncreate() method in your fragment. – Ritt Nov 02 '15 at 20:21
  • I did that, and it does show an option menu, but SHARE is outside of it and still text. Could they have made it any more difficult to do something seemingly so profoundly simple? – samdoj Nov 02 '15 at 20:25
  • it should a share like button on actionbar and on click of think, it should open all options. You are saying you are just seeing a text? – Ritt Nov 02 '15 at 20:27
  • is your code on github or bitbucket? It would be better to have a look. – Ritt Nov 02 '15 at 20:32
  • can you see the action bar in your activity?Check the action bar theme you are applying in the activity. – Ritt Nov 02 '15 at 20:55
  • have a look at this solution http://stackoverflow.com/questions/30284627/how-to-show-and-hide-actionbar-with-appcompat-v-7 – Ritt Nov 02 '15 at 21:01
  • I don't understand. I can see the ActionBar. – samdoj Nov 02 '15 at 21:13