【问题标题】:AppCompatDelegate.setDefaultNightMode doesn't work on physical deviceAppCompatDelegate.setDefaultNightMode 在物理设备上不起作用
【发布时间】:2020-11-30 12:04:33
【问题描述】:

更改夜灯模式在我的模拟器中完美运行,但是当我尝试在物理设备(小米)上执行相同操作时,我得到下一个异常:

E/ActivityInjector: get life cycle exception
    java.lang.ClassCastException: android.os.BinderProxy cannot be cast to android.app.servertransaction.ClientTransaction
        at android.app.ActivityInjector.checkAccessControl(ActivityInjector.java:24)
        at android.app.Activity.onResume(Activity.java:1859)
        at androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:456)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
        at android.app.Activity.performResume(Activity.java:8050)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4269)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4311)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57)
        at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5353)

这是我在活动和演示者中更改应用程序模式的代码:

public class MoviesListActivity extends AppCompatActivity implements MovieListView {

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.darkMode) {
            presenter.changeToDarkMode();
        }
        else if(item.getItemId() == R.id.lightMode) {
            presenter.changeToLightMode();
        }
        return super.onOptionsItemSelected(item);
    }

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

        presenter =  new MoviesListPresenter(this, getApplicationContext());

        if (presenter.getCurrentMode() == 0) {
            presenter.changeToLightMode();
        } else {
            presenter.changeToDarkMode();
        }
    }

    //Other code...
}

演讲者:

public class MoviesListPresenter {
 
    public void changeToLightMode() {
        currentMode = context.getSharedPreferences(Constants.MODE, Context.MODE_PRIVATE);
        currentMode.edit().putInt(Constants.MODE, 0).apply();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

    public void changeToDarkMode() {
        currentMode = context.getSharedPreferences(Constants.MODE, Context.MODE_PRIVATE);
        currentMode.edit().putInt(Constants.MODE, 1).apply();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    public int getCurrentMode() {
        currentMode = context.getSharedPreferences(Constants.MODE, Context.MODE_PRIVATE);
        return currentMode.getInt(Constants.MODE, 0);
    }
}

如果有任何帮助,我将不胜感激!

【问题讨论】:

  • 您的异常与夜间模式完全无关 - 切换模式可能会强制重新创建 Activity 并且您在这种情况下的行为不正确...查看堆栈跟踪和指向的类在那里ResumeActivityItemTransactionExecutorClientTransactionHandler - 显然是从Activity 开始的,当这个Activity 不是新创建的,而是重新创建时它就不起作用

标签: java android android-darkmode


【解决方案1】:

这似乎是在小米固件中调用Activity.recreate()引起的。这个问题也发生在我的库中,它重新创建了应用语言更改的活动。

https://github.com/akexorcist/Localization/issues/89

就我而言,没有应用程序崩溃并且代码正常运行。所以我跳过了这个问题。

相关问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2021-05-11
    • 2022-11-10
    • 2016-12-07
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多