Android的ShareAction提供商空指针 [英] Android ShareAction Provider null pointer

查看:211
本文介绍了Android的ShareAction提供商空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的份额行动提供商空指针,因为它是在应用程序开始预期的图像。我只能稍后提供。

下面是我的code

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){    //充气菜单资源文件。
    。getMenuInflater()膨胀(R.menu.menu,菜单);    //与ShareActionProvider找到菜单项
    菜单项项= menu.findItem(R.id.menu_item_share);    //读取和存储ShareActionProvider
    mShareActionProvider =(ShareActionProvider)item.getActionProvider();    返回true;
}@覆盖
prepareOptionsMenu(菜单菜单)上公共布尔{
    意图shareIntent =新意图(Intent.ACTION_SEND);
    shareIntent.setType(图像/ *);
    URI URI = Uri.fromFile(新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
            getBitmapName()));
    shareIntent.putExtra(Intent.EXTRA_STREAM,URI);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT,的getString(R.string.mailSubject));
    setShareIntent(shareIntent);    返回true;
}//调用更新份额的意图
私人无效setShareIntent(意向shareIntent){
    如果(mShareActionProvider!= NULL){
        mShareActionProvider.setShareIntent(shareIntent);
    }
}


解决方案

嘿维尼特我想你可能需要将其发送到共享的意图之前,你的应用程序初始化你的形象。因此,而不是发送份额意图URI从URI拉图像上创建,然后将其设置使用BM = BitmapFactory.de codeResource(getResources(),R.drawable.image);

不知道这是否会工作或没有,但共享的意图会得到一个空指针异常的唯一原因是来自不存在或者未初始化的图像。

希望这有助于

道格

i am getting null pointer in share action provider because it's expecting an image at start of app. i can provide it only later.

Here is my code

@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);
    }
}

解决方案

Hey vineet I think you may need to initialize your image in your app before sending it to the share intent. So instead of sending the share intent a URI pull the image from the uri in on create then set it up using bm=BitmapFactory.decodeResource(getResources(),R.drawable.image);.

Not sure if this will work or not but the only reason the sharing intent would be getting a null pointer exception would be from an image that doesn't exist or is not initialized.

Hope this helps

Doug

这篇关于Android的ShareAction提供商空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆