2016-04-06 36 views
0

我得到錯誤:錯誤上下文不能應用於adapterview.onitemclicklistener

getFacebookPageURL (android.content.Context) cannot be applied to (android.widget.AdapterView.OnItemClickListener) with this

這裏是我的代碼:

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

嘗試「Activiy.this」而不是「this」 – Sanoop

+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

相關問題