简体   繁体   English

Android DataBinding setVariable()后跟getVariable()调用返回null

[英]Android DataBinding setVariable() followed by getVariable() call returns null

I'm using DataBinding with the following layout. 我正在使用具有以下布局的DataBinding。 I'm calling the setViewModel() method on the binding object. 我正在绑定对象上调用setViewModel()方法。 If I immediately call binding.getViewModel(), it returns null. 如果我立即调用binding.getViewModel(),它将返回null。 See code below: 见下面的代码:

Layout: 布局:

<layout>
<data>
    <variable
        name="viewModel"
        type="reyes.r.christopher.spenderbender.viewmodel.TransactionViewModel"/>
</data>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/viewExpenseListTable"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        >
    </TableLayout>
</ScrollView>
</layout>

Activity: 活动:

public class ViewExpenseListActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityViewExpenseListBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_view_expense_list);

        LocalDatabaseHandler dbh = new LocalDatabaseHandler(this);
        TransactionViewModel viewModel = new TransactionViewModel(dbh);

        binding.setViewModel(viewModel);
        if ( BuildConfig.DEBUG ) {
            if (viewModel == null) {
                throw new AssertionError("Somehow viewModel is null...");
            }
            if(binding.getViewModel() == null) {
                // This Assertion is always thrown
                throw new AssertionError("viewModel is not null, but wasn't set in the binding");
            }
        }
        // Throws NullPointerException
        binding.getViewModel().loadAllExpenses();
    }
    ...
}

You may notice that I'm not using the variable anywhere in the layout. 您可能会注意到我没有在布局中的任何位置使用变量。 I'm trying to programmatically replace the rows in the table with values from the database whenever new rows are added to the database. 每当新行添加到数据库时,我都试图以编程方式用数据库中的值替换表中的行。 That's why I need to get the viewModel from the binding. 这就是我需要从绑定中获取viewModel的原因。

PS: I found this similar question: Android Databinding Adapter Null Pointer Exception . PS:我发现了类似的问题: Android Databinding Adapter Null Pointer Exception This person was getting a Null Pointer Exception when they called the set method, not the get method. 当他们调用set方法而不是get方法时,这个人得到了Null Pointer Exception。 I can call the set method just fine, but the get returns a null object. 我可以调用set方法,但get返回一个null对象。

Turns out, if the variable isn't referenced in the layout, then the generated setters and getters aren't implemented. 事实证明,如果布局中未引用变量,则不会生成生成的setter和getter。

I decided to post this in case anyone else had a similar question in the future, but I discovered this upon investigating the generated file. 我决定发布这个以防万一其他人在将来有类似的问题,但我在调查生成的文件时发现了这一点。 Here's what the getViewModel() and setViewModel() methods look like in the generated ActivityViewExpenseListBinding class: 以下是生成的ActivityViewExpenseListBinding类中getViewModel()和setViewModel()方法的外观:

public void setViewModel(reyes.r.christopher.spenderbender.viewmodel.TransactionViewModel viewModel) {
    // not used, ignore
}
public reyes.r.christopher.spenderbender.viewmodel.TransactionViewModel getViewModel() {
    return null;
}

I need to either reference the viewModel variable from within the layout, or use a different pattern for observing changes. 我需要从布局中引用viewModel变量,或者使用不同的模式来观察更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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