我制作了自定义复合视图:
linkui.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some Text" />
</LinearLayout>
并将其放在activity.xml中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- ....Other components -->
<com.button.LinkUI
android:id="@+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
这是视图的java类,它有一个setter setText
:
public class LinkUI extends LinearLayout {
private TextView textView;
public LinkUI(Context context) {
super(context);
init();
}
public LinkUI(Context context, AttributeSet attrs) {
super(context,attrs);
init();
}
public LinkUI(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setText(String str){
this.textView.setText(str);
}
private void init() {
inflate(getContext(),R.layout.linkui,this); <---
this.textView = (TextView)findViewById(R.id.text);
}
}
根据调试控制台,视图以第二个构造函数public LinkUI(Context context, AttributeSet attrs)
开始,然后在视图膨胀布局init()
时在R.layout.linkui
崩溃,任何人都可以告诉我充气有什么问题xml?
错误:
FATAL EXCEPTION: main
Process: com.site, PID: 13060
android.view.InflateException: Binary XML file line #63: Error inflating class com.button.LinkUI
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.mikepenz.fastadapter.items.AbstractItem.getViewHolder(AbstractItem.java:233)
at com.mikepenz.fastadapter.FastAdapter$OnCreateViewHolderListenerImpl.onPreCreateViewHolder(FastAdapter.java:1628)
at com.mikepenz.fastadapter.FastAdapter.onCreateViewHolder(FastAdapter.java:382)
at com.mikepenz.fastadapter.AbstractAdapter.onCreateViewHolder(AbstractAdapter.java:128)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6073)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5243)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:562)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3374)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2901)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:770)
at android.view.View.measure(View.java:16772)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1662)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16772)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
at android.view.View.measure(View.java:16772)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5139)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widg
我的sdk版本:
compileSdkVersion 25 buildToolsVersion“25.0.0”
答案 0 :(得分:1)
不要在构造函数中调用initUi(),在onFinishInflate()中调用它。