在 Android 11 中加载 Chrome 自定义选项卡时出错
Error on load Chrome Custom Tab in Android 11
Chrome custom tab
在所有其他设备上工作。 Android 11
.
中只有 error
这是我的代码,我是如何将代码放入我的应用程序中的。
ImageView adCustomBannerImage= findViewById(R.id.adCustomBannerImage);
adCustomBannerImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String urlCustomBannerAd= getResources().getString(R.string.urlCustomBannerAdQureka);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent=builder.build();
customTabsIntent.launchUrl(getApplicationContext(),Uri.parse(urlCustomBannerAd));
}
});
当我点击 ImageView
然后这个错误出现 Android 11
2022-03-11 10:22:21.344 3059-3059/com.udai.aadharloan.kredit.finance.pmscheme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.udai.aadharloan.kredit.finance.pmscheme, PID: 3059
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1018)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:425)
at androidx.core.content.ContextCompat$Api16Impl.startActivity(ContextCompat.java:830)
at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:279)
at androidx.browser.customtabs.CustomTabsIntent.launchUrl(CustomTabsIntent.java:376)
at com.udai.aadharloan.kredit.finance.pmscheme.MainActivity.onClick(MainActivity.java:76)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access00(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
在对这个主题进行了大量研究之后,我找到了 3 个有用的页面。但我不是专业开发人员,所以我无法理解这些文档术语。
- Using Custom Tabs with Android 11
- Package visibility in Android 11
- Fulfilling common use cases while having limited package visibility
所以任何开发人员都可以告诉我,我的代码需要更改什么。
提前致谢:)
删除 getApplicationContext() 并传递 your_activityname.this
customTabsIntent.launchUrl(your_activityname.this,Uri.parse(urlCustomBannerAd));
Chrome custom tab
在所有其他设备上工作。 Android 11
.
error
这是我的代码,我是如何将代码放入我的应用程序中的。
ImageView adCustomBannerImage= findViewById(R.id.adCustomBannerImage);
adCustomBannerImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String urlCustomBannerAd= getResources().getString(R.string.urlCustomBannerAdQureka);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent=builder.build();
customTabsIntent.launchUrl(getApplicationContext(),Uri.parse(urlCustomBannerAd));
}
});
当我点击 ImageView
然后这个错误出现 Android 11
2022-03-11 10:22:21.344 3059-3059/com.udai.aadharloan.kredit.finance.pmscheme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.udai.aadharloan.kredit.finance.pmscheme, PID: 3059
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1018)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:425)
at androidx.core.content.ContextCompat$Api16Impl.startActivity(ContextCompat.java:830)
at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:279)
at androidx.browser.customtabs.CustomTabsIntent.launchUrl(CustomTabsIntent.java:376)
at com.udai.aadharloan.kredit.finance.pmscheme.MainActivity.onClick(MainActivity.java:76)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access00(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
在对这个主题进行了大量研究之后,我找到了 3 个有用的页面。但我不是专业开发人员,所以我无法理解这些文档术语。
- Using Custom Tabs with Android 11
- Package visibility in Android 11
- Fulfilling common use cases while having limited package visibility
所以任何开发人员都可以告诉我,我的代码需要更改什么。
提前致谢:)
删除 getApplicationContext() 并传递 your_activityname.this
customTabsIntent.launchUrl(your_activityname.this,Uri.parse(urlCustomBannerAd));