DataBinding错误 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/lifecycle/Observer

在学习DataBinding的时候,App出现奔溃:Rejecting re-init on previously-failed class java.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/lifecycle/Observer;

具体错误日志信息如下:

2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art: Rejecting re-init on previously-failed class java.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/lifecycle/Observer;
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.databinding.ViewDataBinding.() (ViewDataBinding.java:120)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at android.databinding.ViewDataBinding android.databinding.DataBinderMapperImpl.getDataBinder(android.databinding.DataBindingComponent, android.view.View, int) (DataBinderMapperImpl.java:15)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at android.databinding.ViewDataBinding android.databinding.DataBindingUtil.bind(android.databinding.DataBindingComponent, android.view.View, int) (DataBindingUtil.java:199)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at android.databinding.ViewDataBinding android.databinding.DataBindingUtil.bindToAddedViews(android.databinding.DataBindingComponent, android.view.ViewGroup, int, int) (DataBindingUtil.java:327)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at android.databinding.ViewDataBinding android.databinding.DataBindingUtil.setContentView(android.app.Activity, int, android.databinding.DataBindingComponent) (DataBindingUtil.java:306)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at android.databinding.ViewDataBinding android.databinding.DataBindingUtil.setContentView(android.app.Activity, int) (DataBindingUtil.java:284)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void com.hust_twj.zademo.third_part.data_binding.DataBindingActivity.onCreate(android.os.Bundle) (DataBindingActivity.java:17)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6679)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2618)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2726)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1477)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.os.Looper.loop() (Looper.java:154)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-07-23 23:30:01.033 14075-14075/com.hust_twj.zademo I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
2019-07-23 23:30:01.034 14075-14075/com.hust_twj.zademo I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)

相关资源如下:

public class User {

    public String name;

    public int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

}




    
        
    

   
       

       
   



原因:实体类定义的变量age为int类型,而xml中对年龄赋值的时候直接为:android:text="@{user.age}",这就犯了 Java 中的textView.seText(18)一样的错误。正确的姿势为:android:text="@{String.valueOf(user.age)}",等价于 Java 代码 textView.seText(String.valueOf(18))

找到这个原因花了一些时间,主要是对 DataBinding 不熟悉;而且,日志的错误信息也很含糊,不方便定位问题。

你可能感兴趣的:(Android)