相关疑难解决方法(0)

UnsupportedOperationException:试图从与上下文无关的上下文中获取显示

我在实时应用程序上遇到 UnsupportedOperationException 崩溃。所有崩溃都与 Moto Android 11 设备有关。可以看出它与 onKeyUp 有某种关系。但仍然不知道如何重现或解决这个问题。任何帮助,将不胜感激。

Fatal Exception: java.lang.UnsupportedOperationException: Tried to obtain display from a Context not associated with  one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays. Other types of Contexts are typically related to background entities and may return an arbitrary display.
   at android.app.ContextImpl.getDisplay(ContextImpl.java:2580)
   at android.content.ContextWrapper.getDisplay(ContextWrapper.java:1030)
   at android.content.ContextWrapper.getDisplay(ContextWrapper.java:1030)
   at android.app.Activity.onKeyUp(Activity.java:3859)
   at android.view.KeyEvent.dispatch(KeyEvent.java:2866)
   at android.app.Activity.dispatchKeyEvent(Activity.java:4176)
   at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:122)
   at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
   at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:140)
   at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
   at …
Run Code Online (Sandbox Code Playgroud)

android motorola keyup

6
推荐指数
1
解决办法
387
查看次数

以编程方式更改语言(Android N 7.0 - API 24)

我正在使用以下代码在我的应用程序中设置特定语言.语言保存SharedPreferences在应用程序中.它完全可以达到API级别23.由于Android N也SharedPreferences运行良好,它返回正确的语言代码字符串,但它不会更改区域设置(设置手机的默认语言).可能有什么不对?

更新1:当我Log.v("MyLog", config.locale.toString());res.updateConfiguration(config, dm)它返回正确的语言环境后立即使用,但应用程序的语言没有改变.

更新2:我还提到如果我更改区域设置然后重新启动活动(使用新意图并完成旧版本),它会正确更改语言,甚至在轮换后显示正确的语言.但是当我关闭应用程序并再次打开它时,我会得到默认语言.有点奇怪.

public class ActivityMain extends AppCompatActivity {

    //...
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        // Set locale
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        String lang = pref.getString(ActivityMain.LANGUAGE_SAVED, "no_language");
        if (!lang.equals("no_language")) {
            Resources res = context.getResources();
            Locale locale = new Locale(lang);
            Locale.setDefault(locale);
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration config = res.getConfiguration();
            if (Build.VERSION.SDK_INT >= 17) {
                config.setLocale(locale);
            } else {
                config.locale = locale;
            }
        }
        res.updateConfiguration(config, …
Run Code Online (Sandbox Code Playgroud)

android locale android-7.0-nougat

5
推荐指数
1
解决办法
8500
查看次数

执行Espresso测试时更改区域设置

我正在创建一个应该支持阿拉伯语言和RTL布局的简单布局.一切正常.现在我想写一个Espresso测试并断言天气它实际上是否显示翻译的文本.例如,对于阿拉伯语言,它应该显示来自arabic strings.xml的文本.

到目前为止,我尝试将下面的代码作为TestRule.

public void setLocale(Locale locale) {
        Resources resources = InstrumentationRegistry.getTargetContext().getResources();
        Locale.setDefault(locale);
        Configuration config = resources.getConfiguration();
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码更改了布局方向,但不从本地化目录加载资源.

我没有做任何额外的事情,但像http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/

我错过了什么吗?

android localization internationalization android-espresso

5
推荐指数
1
解决办法
995
查看次数

在运行 Android 11 的摩托罗拉设备上打开键盘时应用程序崩溃

我们在运行 Android 11 的摩托罗拉设备上遇到了问题。在其他设备上,即使在 Android 11 上,我们也无法重现此问题。我们已经查看了UnsupportedOperationException:尝试从与某个不相关的上下文获取显示,但该线程是目前不活跃,因为我们没有收到任何回复。

该问题似乎与在 TextInput 字段中打开键盘有关。完整的错误语句如下。

Fatal Exception: java.lang.UnsupportedOperationException

Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays. Other types of Contexts are typically related to background entities and may return an arbitrary display.
Run Code Online (Sandbox Code Playgroud)

android motorola keyboard-events android-context android-11

5
推荐指数
1
解决办法
659
查看次数

以编程方式更改区域设置不适用于 Android 6.0.1

我在将语言更改为 Android 6.0.1 时遇到问题,在新的 Android 版本上更改语言效果很好,但在 6.0.1 上设置默认字符串,而不管设备设置的语言如何。模拟器可以工作,但是当我在三星 J5 上安装 apk 时,它在 Android 6.0.1 上工作,更改语言不起作用。我的问题有什么解决办法吗?谢谢你。

 private void setLocale(String lang) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = 
    getBaseContext().getResources().getConfiguration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

} else {
    Resources resources = getBaseContext().getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(new Locale(lang));
    getBaseContext().getApplicationContext().createConfigurationContext(configuration);
}

// shared pref.
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();

}
Run Code Online (Sandbox Code Playgroud)

java android

3
推荐指数
1
解决办法
4113
查看次数