android获取绑定的类名,android – 从视图类获取绑定

我有一个带有数据绑定布局的CustomView类,它接受一个变量.在包含CustomView的布局中,我想将一个属性传递给CustomView,并让该CustomView将该属性传递给它自己的布局绑定.这就是我所拥有的:

public class CustomView extends LinearLayout

{

public CustomView(Context inContext,AttributeSet inAttrs)

{

super(inContext,inAttrs);

inflate(inContext,R.layout.custom_view,null);

}

@BindingAdapter({"app:variable"})

public static void SetVariable(CustomView inCustomView,VariableType inMyVariable)

{

CustomViewBinding binding = DataBindingUtil.getBinding(inCustomView);

binding.setMyVariable(inMyVariable);

}

}

这崩溃试图从视图中提取绑定.这有可能吗?这是堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'void xxx.databinding.CustomViewBinding.setVariableType(xxx.VariableType)' on a null object reference

at xxx.CustomView.SetDynamicList(CustomView.java:32)

at xxx.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116)

at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)

at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)

at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)

at android.view.Choreographer.doCallbacks(Choreographer.java:670)

at android.view.Choreographer.doFrame(Choreographer.java:603)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5417)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

如果我改变了

DataBindingUtil.getBinding(inCustomView)

DataBindingUtil.bind(inCustomView)

然后我明白了:

java.lang.IllegalArgumentException: View is not a binding layout

at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:166)

at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:140)

at xxx.CustomView.SetDynamicList(CustomView.java:30)

- at xxx.databinding.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116)

at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)

at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)

at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)

at android.view.Choreographer.doCallbacks(Choreographer.java:670)

at android.view.Choreographer.doFrame(Choreographer.java:603)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5417)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这可能意味着布局文件没有为数据绑定格式化,但确实如此.它具有layout元素,以及包含变量和所有内容的数据元素.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值