1

我目前正在构建一个需要共享图像的应用程序。我已经完成了 XML 并且出现了共享按钮,但是您无法单击它。

这是我在 XML 中的按钮代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_item_scan"
          android:icon="@drawable/scanlogo"
          android:showAsAction="ifRoom"
          android:title="Scan" />
    <item android:id="@+id/menu_item_share"
          android:showAsAction="ifRoom"
          android:title="Share"
          android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

这是我膨胀菜单的地方:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
    return true;
}

这一切都有效,它会显示出来,但它是不可点击的。这是我尝试使其可点击的地方:

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.menu_item_share:
                Intent ShareIntent = new Intent();
                String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), QRMap,"QRCode", null);
                Uri bmpUri = Uri.parse(pathofBmp);
                ShareIntent.setAction(Intent.ACTION_SEND);
                ShareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);


  ShareIntent.setType("image/png");
            mShareActionProvider.setShareIntent(ShareIntent);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

任何帮助将非常感激!

4

1 回答 1

1

删除R.id.menu_item_shareonOptionsItemSelected()方法的大小写。将该代码移至应用程序的其他位置,例如,以在显示之前对其onCreateOptionsMenu()进行配置。ShareActionProvider

例如,这是一个ShareActionProvider我的示例项目中配置一个的活动:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.sap;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.widget.ShareActionProvider;

public class MainActivity extends SherlockActivity implements
    ShareActionProvider.OnShareTargetSelectedListener, TextWatcher {
  private ShareActionProvider share=null;
  private Intent shareIntent=new Intent(Intent.ACTION_SEND);
  private EditText editor=null;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    shareIntent.setType("text/plain");
    editor=(EditText)findViewById(R.id.editor);
    editor.addTextChangedListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.actions, menu);

    share=
        (ShareActionProvider)menu.findItem(R.id.share)
                                 .getActionProvider();
    share.setOnShareTargetSelectedListener(this);

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onShareTargetSelected(ShareActionProvider source,
                                       Intent intent) {
    Toast.makeText(this, intent.getComponent().toString(),
                   Toast.LENGTH_LONG).show();

    return(false);
  }

  @Override
  public void afterTextChanged(Editable s) {
    shareIntent.putExtra(Intent.EXTRA_TEXT, editor.getText());
    share.setShareIntent(shareIntent);
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
                                int after) {
    // ignored
  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before,
                            int count) {
    // ignored
  }
}

在我的例子中,这是使用 ActionBarSherlock,但同样的过程适用于本机操作栏。

于 2013-11-03T22:15:28.263 回答