あなたの回答に基づいて質問を更新した後、これが私の .Class です。
public class ShareScreen extends Activity{
private ShareScreenAdapter adapter;
private ViewPager viewPager;
private String[] mThumbIds = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_screen);
viewPager = (ViewPager) findViewById(R.id.pager);
ImageButton sharingButton = new ImageButton(this);
sharingButton.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sharingButton.setImageResource(R.drawable.share);
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new ShareScreenAdapter(ShareScreen.this, mThumbIds);
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareIt();
break;
default:
break;
}
return true;
}
private void shareIt()
{
//sharing implementation here
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
menu/main_share.xml には共有ボタンがあります:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_share"
android:title="Share"
android:showAsAction="always"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
まだ動作していません !問題は、アクションバーの shareButton が機能しないことです。クリックしても何も起こりません!