在从 firebase 回收器视图中获取的 ChromeCustomTab 中打开 url

Opening a url in ChromeCustomTab fetched from firebase recycler view

我正在尝试使用 ChromeCustom Tab 从我的 firebase recyclerview 打开链接,因为我知道它会缩短加载时间。 如果我使用 webview 打开它,它可以完美运行,但是当我尝试在 ChromeCustomTab

上使用它时,我的应用程序停止了
public class VatFees extends AppCompatActivity {

private static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;
    TextView textView_date;
    TextView textView_title;
    TextView textView_decription;
    TextView textView_link;
    ImageView imageView, imageMinimize;
    Button uriButton;
    private final Context context;

public BlogViewHolder(final View itemView) {
        super(itemView);
        mView = itemView;
        context = itemView.getContext();
        textView_date = itemView.findViewById(R.id.textDate);
        textView_title = (TextView) itemView.findViewById(R.id.title);
        textView_decription = (TextView)itemView.findViewById(R.id.description);
        textView_link = (TextView) itemView.findViewById(R.id.link);
        imageView = (ImageView) itemView.findViewById(R.id.image);
        imageMinimize = itemView.findViewById(R.id.minimize);
        uriButton = itemView.findViewById(R.id.urlBtn);

 uriButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String toolTitle=textView_title.getText().toString();
                String topic = textView_link.getText().toString();

               final CustomTabsIntent.Builder builder = new 
CustomTabsIntent.Builder();
                CustomTabsIntent customTabsIntent = builder.build();


//I tried using (Activity)context to get pass the error
//Wrong 1st argument type. Found: 'android.content.Context', required: 'android.app.Activity' but still not working

                customTabsIntent.launchUrl((Activity) context, Uri.parse(topic));

            }
        });

Chrome实现是这样的

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

但在我看来我无法使用 this

logcat 上的错误是

01-22 17:34:39.158 5087-5087/com.vx.camn D/AndroidRuntime: Shutting down VM
01-22 17:34:39.159 5087-5087/com.vx.camn E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.vx.camn, PID: 5087

java.lang.NoSuchMethodError: No static method startActivity(Landroid/app/Activity;Landroid/content/Intent;Landroid/os/Bundle;)V in class Landroid/support/v4/app/ActivityCompat; or its super classes (declaration of 'android.support.v4.app.ActivityCompat' appears in /data/app/com.vx.camn-2/split_lib_dependencies_apk.apk)
                                                                       at android.support.customtabs.CustomTabsIntent.launchUrl(CustomTabsIntent.java:200)
                                                                       at com.vx.camn.VatFees$BlogViewHolder.onClick(VatFees.java:166)
                                                                       at android.view.View.performClick(View.java:6186)
                                                                       at android.view.View$PerformClick.run(View.java:23232)
                                                                       at android.os.Handler.handleCallback(Handler.java:836)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                       at android.os.Looper.loop(Looper.java:203)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6289)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1094)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
01-22 17:34:39.160 339-446/? D/AALService: enableAALEvent: 1 -> 0
01-22 17:34:39.160 339-446/? D/AALService: 01-22 05:34:39.144 BL= 175,CABC= 256, 

itemView.getContext() 返回的上下文不是 Activity。

您需要将 Vatfees activity 的引用传递给您的适配器(适配器的代码未发布),然后在创建它时将其传递给 ViewHolder。

我会对 Activity 使用 Wea​​kReference 以避免泄漏它。

private static class BlogViewHolder extends RecyclerView.ViewHolder {
    ...
    Button uriButton;
    WeakReference<Activity> hostActivityReference;

    public BlogViewHolder(final View itemView, Activity hostActivity) {
      ...
      this.hostActivityReference = new WeakReference(hostActivity);
      uriButton = itemView.findViewById(R.id.urlBtn);

      uriButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Activity hostActivity = this.hostActivityReference;
            if (hostActivity == null) {
              // Activity has already finished?
              return;
            }
            String toolTitle=textView_title.getText().toString();
            String topic = textView_link.getText().toString();

            final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.launchUrl(hostActivity, Uri.parse(url));
        }

    }

作为参考,Plaid 在其FeedAdapter.java.

上实现了类似的东西