我想将共享按钮添加到我的应用程序,该应用程序共享应用程序的Play商店链接。为此,我使用ShareActionProvider。但实施后,我遇到了很多错误。我还尝试更改错误面板中的代码但仍然出现错误。以下是java代码和菜单xml文件
public class KaviAshokActivity extends AppCompatActivity {
private AdView mAdView1;
private Button b1,b2,b3,b4;
private ShareActionProvider mShareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kaviashok);
/* Admob related important code*/
mAdView1 = (AdView) findViewById(R.id.adView1);
com.google.android.gms.ads.AdRequest adRequest = new com.google.android.gms.ads.AdRequest.Builder().build();
mAdView1.loadAd(adRequest);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Kruti_Dev_010.ttf");
b1 = (Button) findViewById(R.id.todaypoem);
b2 = (Button) findViewById(R.id.marathipoems);
b3 = (Button) findViewById(R.id.hindipoems);
b4 = (Button) findViewById(R.id.aboutauthor);
b1.setTypeface(tf);b2.setTypeface(tf);b3.setTypeface(tf);b4.setTypeface(tf);
b1.setTextSize(20);b2.setTextSize(20);b3.setTextSize(20);b4.setTextSize(20);
//b1.setPadding(10, 15, 15, 0);
b1.setText("आज की कविता");b2.setText("मराठी");
b3.setText("हिंदी");b4.setText("परिचय");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Intent i1,sharingIntent;
//noinspection SimplifiableIfStatement
if (id == R.id.developers_id) {
i1 = new Intent(this, DevelopersActivity.class);
startActivity(i1);
}
if (id == R.id.menu_item_share) {
sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
//String shareBody = "Here is the share content body";
String shareBody = "\nLet me recommend you this application\n\n";
shareBody = shareBody + "https://play.google.com/store/apps/details?id=Orion.Soft \n\n";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
mShareActionProvider.setShareIntent(sharingIntent);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
return super.onOptionsItemSelected(item);
}
public void onClickSelection(View view) {
Intent i;
switch (view.getId()) {
case R.id.todaypoem:
i = new Intent(this, TodaysPoemActivity.class);
startActivity(i);
break;
case R.id.marathipoems:
i = new Intent(this, MarathiActivity.class);
startActivity(i);
break;
case R.id.hindipoems:
i = new Intent(this, HindiActivity.class);
startActivity(i);
break;
case R.id.aboutauthor:
i = new Intent(this, AboutAuthorActivity.class);
startActivity(i);
break;
}
}
}
这是菜单xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:kaviashok="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/developers_id"
android:orderInCategory="100"
android:title="About Developers"
kaviashok:showAsAction="never"
/>
<item
android:id="@+id/menu_item_share"
kaviashok:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
这些是错误
08-03 17:39:26.410 31772-31772/? E/AndroidRuntime: FATAL EXCEPTION: main
08-03 17:39:26.410 31772-31772/? E/AndroidRuntime: Process: in.protechlabz.www.kaviashok, PID: 31772
08-03 17:39:26.410 31772-31772/? E/AndroidRuntime: java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
08-03 17:39:26.410 31772-31772/? E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
08-03 17:39:26.410 31772-31772/? E/AndroidRuntime: at in.protechlabz.www.kaviashok.KaviAshokActivity.onCreateOptionsMenu(KaviAshokActivity.java:58)
08-03 17:39:26.410 31772-31772/? E/AndroidRuntime: at android.app.Activity.onCreatePanelMenu(Activity.java:2856)