我尝试使用include
标记将一些自定义视图添加到使用DataBinding的活动布局中。我的自定义视图也使用DataBinding。但是我在申请开始时遇到了崩溃:
05-02 17:30:03.685 12595-12595/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.novachevskyi.databindingtest, PID: 12595
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.novachevskyi.databindingtest/com.novachevskyi.databindingtest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.novachevskyi.databindingtest.databinding.CustomViewBinding.invalidateAll()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.novachevskyi.databindingtest.databinding.CustomViewBinding.invalidateAll()' on a null object reference
at com.novachevskyi.databindingtest.databinding.ActivityMainBinding.invalidateAll(ActivityMainBinding.java:41)
at com.novachevskyi.databindingtest.databinding.ActivityMainBinding.<init>(ActivityMainBinding.java:33)
at com.novachevskyi.databindingtest.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:105)
at android.databinding.DataBinderMapper.getDataBinder(DataBinderMapper.java:11)
at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:185)
at android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:299)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:279)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
at com.novachevskyi.databindingtest.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBindingUtil.setContentView(this, R.layout.activity_main);
}
}
CustomView:
public class CustomView extends LinearLayout {
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override protected void onFinishInflate() {
super.onFinishInflate();
DataBindingUtil.bind(this);
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.novachevskyi.databindingtest.MainActivity">
<include layout="@layout/custom_view"/>
</RelativeLayout>
</layout>
custom_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<com.novachevskyi.databindingtest.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.novachevskyi.databindingtest.CustomView>
</layout>
我还创建了带有示例项目的公共回购: https://github.com/novachevskyi/DataBinding-issue
我正在使用com.android.tools.build:gradle:1.3.1
的数据绑定,现在,当我尝试将构建工具版本升级到com.android.tools.build:gradle:2.1.0
时,我的所有观点都包含此类问题include
xml标记。对于有关上述问题的任何信息,我们将不胜感激。
答案 0 :(得分:2)
我看了一下你的项目并运行它。我认为它崩溃的原因是因为
DataBindingUtil.bind(this)
如果您删除<layout>
,它将停止崩溃。原因是绑定调用正在查找围绕视图的ViewDataBinding
,但它无法找到它,因此会抛出异常。由于CustomView正在调用bind本身,因此它不会被导致throw new IllegalArgumentException("View is not a binding layout")
bind
的任何内容所包围
我不完全确定您尝试实现的目标,但在onFinishInflate
内调用CustomView
将是多余的,因为活动在绑定布局时为您执行此操作为了活动。如果您需要@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
CustomViewBinding binding = DataBindingUtil.findBinding(this);
}
类中的绑定,您可以执行以下操作:
{{1}}
答案 1 :(得分:0)
@ SeptimusX75的答案无效,因为DataBindingUtil.findBinding(this)
在内部调用this.getTag(R.id.dataBinding)
以获取绑定,但是在调用onFinishInflate
时尚未设置标签。
当需要使用绑定时,尝试通过this.getTag(R.id.dataBinding);
获取绑定。
public class CustomView extends LinearLayout {
// ...
public CustomViewBinding binding;
// ...
public void setTitle(final String text) {
if (binding == null) {
binding = Objects.requireNonNull(getTag(R.id.dataBinding));
}
binding.title.setText(text);
}
}
或者,在调用CustomView上的任何方法之前,从活动中手动设置绑定。
public class CustomView extends LinearLayout {
// ...
public CustomViewBinding binding;
// ...
public void setBinding(final CustomViewBinding binding) {
this.binding = binding;
}
}
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomViewBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.customView.getRoot().setBinding(binding.customView);
}
}
答案 2 :(得分:0)
万一其他人犯了我犯下的er..dumb错误...
当我错误地有两个重复的布局文件,一个使用数据绑定,另一个没有使用...时,发生了此错误。
删除重复项为我解决了错误。
答案 3 :(得分:0)
工具栏中的“转到生成”选项,然后选择“重建项目”。