gpt4 book ai didi

在自定义 View 布局中使用包含标记时 Android 数据绑定(bind)崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:57:28 28 4
gpt4 key购买 nike

我正在尝试使用 include 标记将一些自定义 View 添加到我使用 DataBinding 的 Activity 布局中。我的自定义 View 也在使用 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) 

主要 Activity :

public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBindingUtil.setContentView(this, R.layout.activity_main);
}
}

自定义 View :

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>

自定义 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>

我还使用示例项目创建了公共(public)存储库: https://github.com/novachevskyi/DataBinding-issue

我在 com.android.tools.build:gradle:1.3.1 中使用数据绑定(bind),现在,当我尝试将构建工具版本升级到 com.android 时。 tools.build:gradle:2.1.0,我的所有包含 include xml 标记的 View 都面临这样的问题。如果您能提供有关上述问题的任何信息,我们将不胜感激。

最佳答案

我查看了您的项目并运行了它。我认为它崩溃的原因是因为

@Override protected void onFinishInflate() {
super.onFinishInflate();
DataBindingUtil.bind(this);
}

如果删除 DataBindingUtil.bind(this)它会停止崩溃。原因是绑定(bind)调用正在寻找 <layout>围绕 View 但它找不到它所以它抛出异常。由于 CustomView 正在调用自身的绑定(bind),因此它没有被任何导致 ViewDataBinding 的东西包围至 throw new IllegalArgumentException("View is not a binding layout")

我不完全确定你想要实现什么但调用 bind onFinishInflate 内部将是多余的,因为 Activity 在绑定(bind) Activity 的布局时会为您执行此操作。如果您需要 CustomView 内的绑定(bind)您可以执行以下操作:

@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
CustomViewBinding binding = DataBindingUtil.findBinding(this);
}

关于在自定义 View 布局中使用包含标记时 Android 数据绑定(bind)崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985449/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com