简体   繁体   中英

DataBindingUtil.setContentView returns null

I'm trying to convert my (working) application to use databinding, but I keep getting a filthy null pointer exception.

I have an "editActivity" method:

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);
     ...
     ...
}

My "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>

Notably, my "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>

My error is:

 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

This is the error when I attempt to edit something, not when I merely try to view the home page.

My suspicion is that I need to inflate the layout, in a manner similar to the linked solution:

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

However, this approach is thoroughly stuffed by the lack of a "getContentFrame()" in my code.

So what the heck am I doing wrong?

You have to wrap your activity_edit.xml with <layout> tags as well. Otherwise, it is not a valid databinding layout.

So both your xml's should start with:

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

This is how you do dataBinding for Activity :

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
}

This might help someone if they are facing this issue.

My app was working fine, but recently i had to change package name for my app and it does include google-services, but i forgot to modify the google-services.json file.

Crash doesn't mention this, but just shows NullPointerException at binding step. Try this if you are using google-services .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM