gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:01:29 27 4
gpt4 key购买 nike

我正在尝试实现 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/

27 4 0