0

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";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_popup);






        DisplayMetrics DM = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(DM);
        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() {
            @Override
            public void onClick(View v) {










                Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                String facebookUrl = getFacebookPageURL(this);
                facebookIntent.setData(Uri.parse(facebookUrl));
                startActivity(facebookIntent);
            }


        });

//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
Community
  • 1
  • 1
Ahmed Nageeb
  • 121
  • 1
  • 9

2 Answers2

1

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.

Bryan Dunlap
  • 1,156
  • 10
  • 7
1

The problem lies in the below fragment:

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
            String facebookUrl = getFacebookPageURL(this);
            facebookIntent.setData(Uri.parse(facebookUrl));
            startActivity(facebookIntent);
        }

    });

when you call getFacebookPageURL you are inside of the anonymous class of View.OnClickListener type thus when you pass "this" you are passing instance of View.OnClickListener class, but getFacebookPageURL accepts Context object. Solution for your problem is either to use

getFacebookPageURL(info_popup.this)

or create field for Context instance like:

private Context mContext;
public class info_popup extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        ...
        getFacebookPageURL(mContext)
}

At last but not least it is good practice to use camelcase notation when declaring the object names so in your case info_popup should be format to InfoPopup.

Tomek Gozdek
  • 206
  • 2
  • 5