我无法使数据绑定在BottomSheetDialog布局上正常工作。详细信息如下:
var的定义和设置:
private lateinit var myDrawerBinding: MyDrawerBinding
myDrawerBinding = MyDrawerBinding.bind(myDrawerContent) // crashes on this line
然后以这种方式设置并显示它(尽管它永远不会到达这一点)
myDrawerBinding.viewModel = theViewModel
val bottomSheet = BottomSheetDialog(context)
bottomSheet.setContentView(myDrawerBinding.myDrawerContent)
bottomSheet.show()
以下是XML布局的代码段(my_drawer.xml):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<variable name="viewModel" type="path.to.my.viewModel"/>
</data>
<RelativeLayout
android:id="@+id/myDrawerContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<View
android:layout_width="50dp"
android:layout_height="3dp"
android:visibility="@{viewModel.shouldShowView() ? View.VISIBLE : View.GONE}"/>
....
当它调用上面的.bind()方法时发生崩溃,并且错误是:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.view.View.getTag()' on a null object reference
与我在同一Fragment中显示的单独的DrawerLayout相同的功能完全相同,但是由于某些原因,BottomSheetDialog布局出现问题。
答案 0 :(得分:0)
最终找到了解决办法。我不得不将此视图与DrawerLayout稍有不同,尽管我感觉这种方法也可能适用于该视图。
这是绑定设置:
class Slider extends Component {
constructor(props){
super(props);
this.state={
active: 0,
length: props.items.length
}
}
next = e => {
let nextImage = this.state.active+1;
if(nextImage<this.state.length){
this.setState({active: nextImage});
}
}
prev = e => {
let prevImage = this.state.active-1;
if(prevImage>=0){
this.setState({active: prevImage});
}
}
goTo = ind => this.setState({active:ind});
render(){
return (
<div>
<div className="slideshow-container">
<div className="mySlides fade">
<div className="numbertext">{this.state.active + 1} / {this.state.length}</div>
<img src={this.props.items[this.state.active].img} height="300"/>
<div className="text">{this.props.items[this.state.active].title}</div>
</div>
<a className="prev" onClick={this.prev} style={{backgroundColor:'#ccc', padding:'10px'}}>❮</a>
<a className="next" onClick={this.next} style={{backgroundColor:'#ccc', padding:'10px', float:'right'}}>❯</a>
</div>
<div className="dots">
{this.props.items.map((n, i) => (
<span
key={n.id}
className={`dot ${i === this.state.active ? 'active' : ''}`}
onClick={()=>this.goTo(i)}
></span>
))}
</div>
</div>
);
}
}
然后显示视图:
val myDrawerView = layoutInflater.inflate(R.layout.my_drawer, null)
val binding = MyDrawerBinding.inflate(layoutInflater, myDrawerView as ViewGroup, false)
binding.viewModel = theViewModel
现在就像魅力一样!