簡體   English   中英

錯誤上下文無法應用於adapterview.onitemclicklistener

[英]error context cannot be applied to adapterview.onitemclicklistener

我得到錯誤:

使用此方法無法將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);    

嘗試這樣:

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.OnItemClickListenerActivity類。 而且AdapterView.OnItemClickListener不會擴展Context

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM