单击ImageView时出现java null指针异常

时间:2013-07-09 19:23:31

标签: android onclick nullpointerexception

Iam为twitter设置一个共享按钮,以便从我的drawable共享图片以后动态更改我从网上获取此代码并将其粘贴到我的应用程序中我得到以下错误空指针异常并且无法找出任何帮助非常感谢谢谢。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你能说出它给NullPoitnerException的确切位置吗?

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

File filePath = context.getFileStreamPath("placeholder.jpg");
share("com.twitter.android",filePath.toString());