appcompatdelegate.setdefaultnightmode在物理设备上不工作

tp5buhyn  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(605)

更改夜光模式在我的模拟器中非常有效,但当我尝试在物理设备(小米)上执行相同操作时,我遇到了下一个例外:

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)

以下是我在activity and presenter中更改应用程序模式的代码:

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);
    }
}

我将感谢任何帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题