0

我正在为 twitter 设置一个共享按钮,以便稍后从我的可绘制对象中共享一个图片以动态更改我从网上获取此代码并将其粘贴到我的应用程序中我收到以下错误空指针异常并且无法弄清楚任何帮助将不胜感激谢谢你。

enter code here

 ImageView twittersharing =(ImageView)findViewById(R.id.twitter_sharing);
    twittersharing.setOnClickListener(new View.OnClickListener() 
    {
        private Context context;

        @Override
        public void onClick(View arg0) 
        {
            //Toast.makeText(getBaseContext(), "On click", Toast.LENGTH_SHORT).show();
            // TODO Auto-generated method stub
            File filePath = context.getFileStreamPath("placeholder.jpg");
            share("com.twitter.android",filePath.toString());
        }
    });

      public void share(String nameApp, String imagePath) 
{
    try
    {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpg");
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty())
        {
            for (ResolveInfo info : resInfo) 
            {
                Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpg"); // put here your mime type
                if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo");
                 targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by ME");
                    targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),  "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,  targetedShareIntents.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        }
    }
    catch(Exception e)
     {
         Log.v("VM","Exception while sending image on" + nameApp + " "+  e.getMessage());
     }
 }
4

2 回答 2

0

尝试使用以下方法更改图片: twitterSharing.setImageDrawable(R.drawable.placeholder);

于 2013-07-09T19:34:57.497 回答
0

你能说出它在哪里给出 NullPoitnerException 吗?

另外,您的设备中确实有此处提到的图像(placeholder.jpg),对吗?

File filePath = context.getFileStreamPath("placeholder.jpg");
share("com.twitter.android",filePath.toString());
于 2013-07-10T05:26:12.520 回答