android分享按钮不起作用

时间:2016-04-11 20:43:08

标签: android android-studio menu share shareactionprovider

我有一个DetailsActivity包含简单的字符串,我尝试与共享操作提供程序共享它,但是,共享按钮出现在操作栏中,但在单击时不会出现问题。

这是我的代码:

公共类DetailsActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new DetailFragment())
                .commit();
    }
}


@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_details, menu);
    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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        startActivity(new Intent(this, SettingActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

包含简单视图的占位符片段。

    public static class DetailFragment extends Fragment {

        private static final String LOG_TAG = DetailFragment.class.getSimpleName();

        private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
        private String BookName;
        private String BookDate;
        private Integer BookImage;
        private ImageView imageView;
        private TextView tv;
        private TextView tv2;;
        public DetailFragment() {
            setHasOptionsMenu(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View Rootview = inflater.inflate(R.layout.fragment_details, container, false);

                Intent DetailsIntent = getActivity().getIntent();
                BookName = DetailsIntent.getExtras().getString("Book Name");
                BookImage = DetailsIntent.getExtras().getInt("Book Image");
                BookDate = DetailsIntent.getExtras().getString("Book Date");
                imageView = (ImageView)Rootview.findViewById(R.id.imageView3w);
                tv = (TextView)Rootview.findViewById(R.id.textView);
                tv2 = (TextView)Rootview.findViewById(R.id.textView2);
                imageView.setImageResource(BookImage);
                tv2.setText(BookDate);
                tv.setText(BookName);



            return Rootview;
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // Inflate the menu; this adds items to the action bar if it is present.
            inflater.inflate(R.menu.details_fragment, menu);

            // Retrieve the share menu item
            MenuItem menuItem = menu.findItem(R.id.action_share);

            // Get the provider and hold onto it to set/change the share intent.
            ShareActionProvider mShareActionProvider =
                    (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

            // Attach an intent to this ShareActionProvider.  You can update this at any time,
            // like when the user selects a new piece of data they might like to share.
            if (mShareActionProvider != null ) {
                mShareActionProvider.setShareIntent(createShareForecastIntent());
            } else {
                Log.d(LOG_TAG, "Share Action Provider is null?");
            }
        }

        private Intent  createShareForecastIntent() {
            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,
                    BookName + FORECAST_SHARE_HASHTAG);
           return  shareIntent;
        }
    }
}

0 个答案:

没有答案