SeekBar DataBinding表达式

时间:2017-02-28 22:29:15

标签: android android-databinding

我正在尝试使用lambda表达式为SeekBar onProgressChanged调用一个方法,但我得到了这个异常:

android.content.res.Resources$NotFoundException: String resource ID #0x0
    at android.content.res.Resources.getText(Resources.java:336)
    at android.widget.TextView.setText(TextView.java:4465)
    at br.com.caradviser.app.databinding.FragmentFilterBinding.executeBindings(FragmentFilterBinding.java:476)
    at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
    at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:172)
    at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:142)
    at android.view.View.dispatchAttachedToWindow(View.java:14714)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2962)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2969)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1599)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6708)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
    at android.view.Choreographer.doCallbacks(Choreographer.java:696)
    at android.view.Choreographer.doFrame(Choreographer.java:631)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5728)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="br.com.app.fragments.FilterFragment">

    <data>

        <variable
            name="filter"
            type="br.com.app.model.Filter"/>

        <variable
            name="presenter"
            type="br.com.app.contract.FilterContract.Presenter" />
    </data>

    <RelativeLayout
        android:id="@+id/content_filter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_filter">


<!--
Some widgets omitted for brevity
-->

    <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/separator_2"
                android:text="Nota Mínima" />

            <SeekBar
                android:id="@+id/bar_min_rating"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textView3"
                android:max="5"
                android:progress="@={filter.minRating}"
                android:onProgressChanged="@{(seekBar, progress, fromUser) -> presenter.minRatingChanged(seekBar)}" />

            <TextView
                android:id="@+id/text_rating_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/bar_min_rating"
                android:text="@{filter.minRating}"
                android:translationX="12dp" />

    </RelativeLayout>
</layout>

片段代码:

public class FilterFragment extends Fragment
        implements FilterContract.View {

    private FragmentFilterBinding mViewDataBinding;
    private FilterContract.Presenter mPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_filter, container, false);

        mViewDataBinding = FragmentFilterBinding.bind(view);

        Filter model = new Filter();
        mViewDataBinding.setFilter(model);

        return view;
    }

}

主讲人:

public class FilterPresenter implements FilterContract.Presenter {

    private final FilterContract.View mView;

    public FilterPresenter(FilterContract.View mView) {
        this.mView = mView;

        mView.setPresenter(this);
    }

    public void start() {

    }

    @Override
    public void maxDistanceChanged(int position) {

    }

    @Override
    public void minRatingChanged(SeekBar seekBar) {
        Log.d("minRatingChanged", String.valueOf(seekBar.getProgress()));
    }
}

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:3)

异常不是由SeekBar绑定问题造成的android:text="@{filter.minRating}" 此外,我无法确定您在哪里设置filter

由于您没有发布br.com.app.model.Filter的代码,我猜minRating是一个整数。 因此绑定需要一个字符串资源,在您的情况下为id 0。

只需将minRating的类型更改为String