我正在为 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());
}
}