Android ShareAction Provider空指针

时间:2013-05-01 11:21:37

标签: android shareactionprovider

我在共享操作提供程序中获取空指针,因为它期待应用程序启动时的图像。我可以稍后提供。

这是我的代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.menu, 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 onPrepareOptionsMenu(Menu menu) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    Uri uri = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            getBitmapName()));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mailSubject));
    setShareIntent(shareIntent);

    return true;
}



// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}

1 个答案:

答案 0 :(得分:0)

嘿Vineet我认为您可能需要在将应用程序发送到共享意图之前在应用程序中初始化您的图像。因此,不是发送共享意图,而是从创建中的uri中提取URI,然后使用bm = BitmapFactory.decodeResource(getResources(),R.drawable.image);来设置它。

不确定这是否有效,但共享意图获得空指针异常的唯一原因是来自不存在或未初始化的图像。

希望这有帮助

道格