Ahmed Nageeb Ahmed Nageeb - 1 year ago 125
Android Question

android error on facebook page open with a button

i want to refer to my facebook page in my app
so when the user press the button he will be directed to my fb page in fb app
or else he ill be directed to my fb page in browser
i dont know how this works so i coped the code
from this post

enter link description here

but i get error in (this) and i cant seem to understand the problem
this is the code

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
public class info_popup extends Activity {
public static String FACEBOOK_URL = "https://www.facebook.com/ahmednageebobiad";
public static String FACEBOOK_PAGE_ID = "Ahmed Nageeb";
protected void onCreate(Bundle savedInstanceState) {

DisplayMetrics DM = new DisplayMetrics();
int Width = DM.widthPixels;
int Hieght = DM.heightPixels;
getWindow().setLayout((int) (Width * .8), (int) (Hieght * .6));

Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);


//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.katana", 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

and here is the error

Error:(50, 38) error: method getFacebookPageURL in class info_popup cannot be applied to given types;
required: Context
found: <anonymous OnClickListener>
reason: actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion

Answer Source

Your assignment:

String facebookUrl = getFacebookPageURL(this);

In scope, this refers to your anonymous View.OnClickListener.

You need to change your assignment statement to:

String facebookUrl = getFacebookPageURL(info_popup.this);

... to correctly refer to your Activity instance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download