我正在尝试使用针对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()));
    }
}


我的代码有什么问题?

最佳答案

SeekBar绑定不是引起异常的问题是android:text="@{filter.minRating}"
我也看不到你在哪里设置filter

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

因此,只需将minRating的类型更改为String

关于android - SeekBar DataBinding表达式上的资源NotFoundException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42519935/

10-11 01:29
查看更多