gpt4 book ai didi

java - DataBindingUtil.setContentView 返回 null

转载 作者:行者123 更新时间:2023-11-30 06:33:29 24 4
gpt4 key购买 nike

我正在尝试将我的(工作)应用程序转换为使用数据绑定(bind),但我不断收到肮脏的空指针异常。

我有一个“editActivity”方法:

public class EditActivity extends AppCompatActivity {
// Removed per conversion to data binding
// private EditText nameText;
// private EditText descriptionText;
// private EditText priorityText;

private TodoItem item;

ContentEditBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_edit);
...
...
}

我的“activity_edit.xml”

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".TodoListActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_edit" />

</android.support.design.widget.CoordinatorLayout>

值得注意的是,我的“content_edit.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">
<data>
<variable name="TodoItem" type="com.javadude.todostarter.TodoItem"/>
</data>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/activity_edit"
tools:context=".EditActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<TextView android:text="@string/name"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/name"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={TodoItem.name}" />
<TextView android:text="@string/description"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/description"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:inputType="text|textAutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={TodoItem.description}" />
<TextView android:text="@string/priority"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/priority"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='@={TodoItem.priority + ""}' />
</LinearLayout>
</ScrollView>
</layout>

我的错误是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javadude.todostarter/com.javadude.todostarter.EditActivity}:

java.lang.NullPointerException: Attempt to read from field 'android.widget.EditText com.javadude.todostarter.databinding.ContentEditBinding.name' on a null object reference

这是当我尝试编辑某些内容时出现的错误,而不是当我仅仅尝试查看主页时出现的错误。

我怀疑我需要 inflate布局,其方式类似于链接的解决方案:

ActivityOrderOnePaneBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_order_one_pane, getContentFrame(), false);
setContentView(binding.getRoot());

但是,由于我的代码中缺少“getContentFrame()”,这种方法完全被塞满了。

那么我到底做错了什么?

最佳答案

你必须包裹你的 activity_edit.xml<layout>标签也是如此。否则,它不是有效的数据绑定(bind)布局。

所以你的 xml 应该以:

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

关于java - DataBindingUtil.setContentView 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624786/

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