安卓 ShareAction Provider 空指针异常

3
我在共享操作提供程序中得到了空指针,因为它期望应用程序开始时就有图像。我只能稍后提供它。
这是我的代码:
@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);
    }
}

你能发布完整的代码(包括 onCreate)吗?NPE可能是由于在初始化完成之前调用了 setShareIntent 导致的。 - dumbfingers
@vineet,你最终弄清楚这里发生了什么了吗? - Michael Alan Huff
1个回答

0

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

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

希望这可以帮助到你。

道格


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接