应用程序有时会在 androidx.appcompat.widget.ContentFrameLayout.setDecorPadding() 方法上崩溃

H. *_*lba 2 android androidx

有时,我的应用程序打开时会发生崩溃:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
       at androidx.appcompat.app.AppCompatDelegateImpl.applyFixedSizeWindow(AppCompatDelegateImpl.java:1026)
       at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:820)
       at androidx.appcompat.app.AppCompatDelegateImpl.onPostCreate(AppCompatDelegateImpl.java:527)
       at androidx.appcompat.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:127)
       at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1381)
       at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3499)
       at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
       at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7814)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,堆栈跟踪中没有引用我的代码中的某些内容,因此因此我无法找出问题所在。这次事故的原因可能是什么?

Ano*_*ous 5

就我而言,当我加载第一个屏幕时,我在生产中遇到了此错误。我也使用 androidx 并且没有支持库。有时这种崩溃看起来像:

Caused by java.lang.RuntimeException
Window couldn't find content container view ***.onCreate
Run Code Online (Sandbox Code Playgroud)

不幸的是,我仍然不明白错误到底是什么,但我找到了这篇文章。我将onCreate方法更改为:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
     getWindow().getDecorView();
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main_screen);
}
Run Code Online (Sandbox Code Playgroud)

getWindow().getDecorView();之前刚刚添加super.onCreate(savedInstanceState);,它解决了我的问题。