错误上下文无法应用于adapterview.onitemclicklistener

时间:2016-04-06 12:17:03

标签: android facebook

我收到错误:

  使用此

getFacebookPageURL(android.content.Context)无法应用于(android.widget.AdapterView.OnItemClickListener)

这是我的代码:

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.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
    }
}    

然后按如下方式启动意图:

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

1 个答案:

答案 0 :(得分:0)

试试这样:

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(MainActivity.this); //name of your activity
facebookIntent.setData(Uri.parse(facebookUrl));
MainActivity.this.startActivity(facebookIntent);    

因为this只引用AdapterView.OnItemClickListener而不是Activity类。并AdapterView.OnItemClickListener不会延伸Context