该代码适用于pre-oreo设备,但Crashlytics称它崩溃了 android 8设备
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getAppComponent().inject(this);
binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
}
stacktrace
java.lang.NullPointerException:尝试调用虚方法' int android.view.ViewGroup.getChildCount()'在空对象引用上
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getChildCount()' on a null object reference
at android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:295)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:279)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
at com.myapp.MyActivity.onCreate(MyActivity.java:59)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:9)
在8上有类似的崩溃与完全相同的堆栈跟踪。
您是否尝试过使用:
binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity, null, false);
setContentView(binding.getRoot());
上述代码与调用之间的区别
DataBindingUtil.setContentView(...);
是inflate()
直接返回View
,后来传递给DataBindingUtils::bindToAddedViews
。
如果DataBindingUtil.setContentView
,则使用以下逻辑
activity.setContentView(layoutId);
View decorView = activity.getWindow().getDecorView();
ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
似乎
ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
只是NULL
...