I am trying to set an on click listener on an ImageView, which will open the Facebook app on a specific page. The variable 'facebook' holds the URL of the Facebook page I want to open My code for the on click listener is as follows:
imgFb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (facebook != null) {
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(context);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
}
}
});
and the method getFacebookUrl() is as follows:
//Open club's Facebook page
public String getFacebookPageURL(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) { //newer versions of fb app
return "fb://facewebmodal/f?href=" + facebook;
} else { //older versions of fb app
return "fb://page/" + facebook;
}
} catch (PackageManager.NameNotFoundException e) {
return facebook; //normal web url
}
}
This code is not working for me. I have null pointer exceptions in the following two lines:
PackageManager packageManager = context.getPackageManager();
String facebookUrl = getFacebookPageURL(context);
I think it is something to do with the context variable. I don't really understand it and I'm not sure exactly what to put in its place so I used the code from Open Facebook Page in Facebook App (if installed) on Android. Could anyone help me with this?