我尝试使用v7.support库在Android应用中实现共享按钮,而不使用它们。我正在尝试使用此代码,但它会在getActionProvider上抛出NPE:
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);
这是我的相关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>
LogCat堆栈在这里:
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
答案 0 :(得分:0)
item
在这里为空。
而不是:
MenuItem item = (MenuItem) findViewById(R.id.menu_item_share);
使用它:
MenuItem item = (MenuItem) menu.findItem(R.id.menu_item_share);
此外,它是app:actionProviderClass
而不是android:actionProviderClass
。
答案 1 :(得分:0)
我建议使用支持库中的类:
<item
android:id="@+id/menu_item_share"
android:title="Share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
Activity
必须扩展ActionBarActivity
:MyActivity extends ActionBarActivity
正确的导入:
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;
答案 2 :(得分:0)
在shareProvider布局中使用它。
<?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>
然后在您的活动或片段中,覆盖此方法..
@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;
}
这里memberBean是我从中获取数据的java bean。您可以根据自己的要求进行更改。