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?
I think You do not declare your context in main body, You have to add,
context=getApplicationContext();
or
context=YourActivity.this;
to your body.
This is worked for me.
Button btn;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
context=getApplicationContext();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isAppInstalled()) {
Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(context);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
} else {
Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
}
}
});
}
public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";
//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.orca", 0).versionCode;
if (versionCode >= 3002850) { //newer versions of fb app
return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
} else { //older versions of fb app
return "fb://page/" + FACEBOOK_PAGE_ID;
}
} catch (PackageManager.NameNotFoundException e) {
return FACEBOOK_URL; //normal web url
}
}
public boolean isAppInstalled() {
try {
getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
}