2017-07-24 48 views
0

我有一個適配器活動extends RecyclerView.Adapter<UserProfileAdapter.ViewHolder>。問題是,當用戶點擊列表中的項目之一的「自動連接」,我得到以下異常:AndroidRuntimeException當在活動中單擊URL時

在我UserProfileActivity,我實例化我的適配器像他:

UserProfileAdapter adapter = new UserProfileAdapter(getApplicationContext(), posts); 

在適配器,我找回這樣的背景下:

private Context mContext; 
    private List<ParseObject> mYeets; 
    private UserProfileAdapter adapter; 

    public UserProfileAdapter(Context context, List<ParseObject> yeets) { 
     super(); 

     this.mYeets = yeets; 
     this.mContext = context; 
     this.adapter = this; 
    } 

enter image description here

我怎樣才能確保自動鏈接文本不會產生這個錯誤?由於沒有與鏈接/意圖關聯的代碼,我甚至可以做什麼?

+1

你截圖的消息中有哪些不清楚? –

+0

事實上,沒有實際的意圖啓動鏈接。它是一個Recycler列表項中的「自動鏈接」文本。所以我只是不能去爲這個意圖添加一個標誌。 – santafebound

回答

2

android.util.AndroidRuntimeException被拋出是因爲在您的應用程序中使用應用程序上下文來啓動沒有標記FLAG_ACTIVITY_NEW_TASK的活動。 Android Runtime不允許這種操作,因爲沒有Stack來添加這個新的Activity,並且FLAG_ACTIVITY_NEW_TASK標誌對於在應用程序中創建新的Stack很重要。

要解決該問題,您可以將活動上下文傳遞給Adapter而不是Application Context(即:使用this)。

例如爲:UserProfileAdapter adapter = new UserProfileAdapter(this, posts);

良好實踐:始終使用活動上下文與UI元素的工作。

相關問題