android - Chrome 自定义标签 : Why is CustomTabsClient. bindCustomTabsService() 无法正常工作?

标签 android chrome-custom-tabs

我正在尝试实现 chrome 自定义选项卡,但出现以下运行时错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nirvan.customtabsexample/com.example.nirvan.customtabsexample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.customtabs.CustomTabsClient.warmup(long)' on a null object reference

这是我的代码:

 CustomTabsClient mClient;
    String packageName = "com.android.chrome";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Binds to the service.
        CustomTabsClient.bindCustomTabsService(this, packageName, new CustomTabsServiceConnection() {
            @Override
            public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
                // mClient is now valid.
                Log.e("TAG","onCustumServiceConnected");
                mClient = client;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // mClient is no longer valid. This also invalidates sessions.
                Log.e("TAG","onServiceDisconnected");
                mClient = null;
            }
        });


        mClient.warmup(0);


        CustomTabsSession session = mClient.newSession(new CustomTabsCallback());
        session.mayLaunchUrl(Uri.parse("https://www.google.com"), null, null);


        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "https://www.facebook.com/";
                CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
                CustomTabsIntent customTabsIntent = builder.build();
                customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
            }
        });






    }

应用程序一启动就崩溃了。此外,我没有获得 2 个日志输出中的任何一个,因此永远不会调用 CustomTabsClient.bindCustomTabsService() 下的两个方法。可能是什么问题? 我认为这是我传递给 CustomTabsClient.bindCustomTabsService() 的包名称。我不知道要传递什么,所以我传递了 "com.android.chrome"。这是哪里出了问题?

最佳答案

您在以下行收到 NullPointerException:

mClient.warmup(0);

原因是一旦调用 CustomTabsClient.bindCustomTabsService,回调将异步运行。调用预热时,没有足够的时间让服务连接并为 mClient 分配值。将预热调用和 mayLaunchUrl 移动到 onConnected 回调内部,它应该可以工作。

    // Binds to the service.
    CustomTabsClient.bindCustomTabsService(this, packageName, new CustomTabsServiceConnection() {
        @Override
        public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
            // mClient is now valid.
            Log.e("TAG","onCustomTabsServiceConnected");
            mClient = client;
            mClient.warmup(0);
            CustomTabsSession session = mClient.newSession(new CustomTabsCallback());
            session.mayLaunchUrl(Uri.parse("https://www.google.com"), null, null);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // mClient is no longer valid. This also invalidates sessions.
            Log.e("TAG","onServiceDisconnected");
            mClient = null;
        }
    });

关于android - Chrome 自定义标签 : Why is CustomTabsClient. bindCustomTabsService() 无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44464747/

相关文章:

安卓 : clickable Image horizontal slider using kotlin

android - 在 android 中的 Cursor 中创建排序顺序的最佳方法?

android - 如何将wit.ai与ogg文件一起使用

android - 在 webview 中重新加载 url 后应用程序崩溃

android - 在 android studio 中点击 google maps 中的标记时打开 alertdialog

android - 如何使用 https 方案在 customtabs 上使用 Intent 过滤器?

android - 在未首次启动 Chrome beta 的情况下启动 Chrome 自定义选项卡示例

google-chrome - Chrome 自定义标签 cookie、存储 API 和服务 worker

android - Chrome 自定义选项卡 - setCloseButtonIcon() 不更改图标

chrome-custom-tabs - 使用 chrome 自定义选项卡时是否可以添加自定义请求 header ?