相关疑难解决方法(0)

Android数据绑定@BindingConversion失败,int为string

尝试为int创建一个@BindingConversion时遇到一个神秘的问题.
以下代码适用于浮点数到字符串:

XML:

...
<variable
        name="myViewModel"
        type="... .SomeModel" />
...
<TextView
            style="@style/StyleStuff"
            android:text="@{myViewModel.number}" />
Run Code Online (Sandbox Code Playgroud)

码:

public class SomeModel {
    public ObservableFloat number = new ObservableFloat();
}
Run Code Online (Sandbox Code Playgroud)

和设置:

viewModel.number.set(3.14f);
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试对字符串进行同样的操作,我会崩溃.

 public ObservableInt number = new ObservableInt();
Run Code Online (Sandbox Code Playgroud)

viewModel.number.set(42);
Run Code Online (Sandbox Code Playgroud)

我得到以下内容:

FATAL EXCEPTION: main
Process: ...myapplication, PID: 14311
android.content.res.Resources$NotFoundException: String resource ID #0xfa0
    at android.content.res.Resources.getText(Resources.java:1123)
    at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
    at android.widget.TextView.setText(TextView.java:4816)
    at ...executeBindings(ActivityAdaptersBinding.java:336)
    at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢!

data-binding android android-databinding

39
推荐指数
6
解决办法
2万
查看次数

找不到资源TextView

我正在迈出Android的第一步,我开始使用一个非常简单的应用程序,它通过编织模式跟踪进度,并显示相关行的说明.

我想以编程方式更新几个TextView对象.但是,使用getViewById()似乎无法正确识别它们并且应用程序崩溃.

在Google上进行搜索后,似乎布局XML中的XML命名空间有时会出现问题,但我看起来还不错.这可能与范围有关吗?

instructions.java(这是唯一的活动)

package uk.co.oketchup.blanketsquare;

import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.View;

public class instructions extends Activity
{
    private int mRow;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* retrieve row from shared preferences, or start from zero if nothing there */
        SharedPreferences settings = getPreferences(MODE_PRIVATE);
        mRow = settings.getInt("row",0);

        setContentView(R.layout.main);

        /* associate onClick listeners with the two buttons */
        final Button btnIncrement …
Run Code Online (Sandbox Code Playgroud)

android textview

18
推荐指数
2
解决办法
1万
查看次数

数据绑定:资源$ NotFoundException当object的属性为int时

我正在尝试使用数据绑定.如果我使用具有字符串属性的对象,它是正常工作,但在这种情况下,我使用int,它不起作用.我有对象用户:

public class User extends BaseObservable{
        public int age;
        ......


        public User() {}

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
         ...

    }
Run Code Online (Sandbox Code Playgroud)

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="user" type="com.example.bindingview.User"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.age}"/>
    </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

问题是TextView不能包含int的age文本.如果我从int更改为string of age属性,它工作正常.我该怎么做才能避免这个问题?

data-binding binding android

12
推荐指数
1
解决办法
2877
查看次数