gpt4 book ai didi

android - 自定义 View 中的 ViewStub 返回 'ViewStub must have a valid layoutResource'

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:15 26 4
gpt4 key购买 nike

我实现了这样的自定义 View :

public class HeaderView extends LinearLayout {

@Bind(R.id.bill_summary_layout)
LinearLayout summaryLayout;
@Bind(R.id.bill_balance)
TextView balance;
@Bind(R.id.bill_closedate)
TextView closedDate;
@Bind(R.id.bill_status)
TextView status;
@Bind(R.id.bill_partial_overdue)
ViewStub partialOverdueView;
@Bind(R.id.bill_partial_closed)
ViewStub partialClosedView;
@Bind(R.id.bill_partial_open)
ViewStub partialOpenView;

Bill bill;

public HeaderView(Context context) {
super(context);
initUI(context);
}

public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
initUI(context);
}

public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initUI(context);
}

private void initUI(Context context) {
LayoutInflater.from(context).inflate(R.layout.header_view, this);
}

public Bill getBill() {
return bill;
}

public void setBill(Bill bill) {
this.bill = bill;
notifyMustRebind();
}

public void notifyMustRebind() {
setupUI();
}

private void setupUI() {
if(this.bill == null)
throw new RuntimeException("O objeto bill tem que estar setado e nao ser nulo. Talvez esqueceu de chamar o setBill?");

switch (bill.getState()) {
case OVERDUE:
setOverdueState();
break;
case OPEN:
setOpenState();
break;
case CLOSED:
setClosedState();
break;
case FUTURE:
setFutureState();
break;
}
}

private void setOverdueState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.overdue));
LayoutInflater.from(getContext()).inflate(R.layout.partial_overdue_view, this, true);
partialOverdueView.inflate();
}

private void setOpenState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.open));
partialOpenView.inflate();
}

private void setClosedState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.closed));
partialClosedView.inflate();
}

private void setFutureState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.future));
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
ButterKnife.unbind(this);
}
}

header_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/bill_summary_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_content_arrow"
android:backgroundTint="@color/colorAccent"
android:orientation="vertical"
android:paddingBottom="@dimen/header_margin"
android:paddingTop="@dimen/header_margin"
>

<TextView
android:id="@+id/bill_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="@dimen/header_text_margin"
android:text="R$ 2.300,10"
android:textColor="@android:color/white"
android:textSize="22sp"
android:textStyle="bold"
/>

<TextView
android:id="@+id/bill_closedate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="@dimen/header_text_margin"
android:text="Vencimento 15 MAI"
android:textColor="@android:color/white"
android:textSize="18sp"
/>

<TextView
android:id="@+id/bill_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="@dimen/header_text_margin"
android:text="Fechamento em 5 de Julho"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="12sp"
/>
</LinearLayout>

<ViewStub
android:id="@+id/bill_partial_overdue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/partial_overdue_view"
/>

<ViewStub
android:id="@+id/bill_partial_closed"
layout="@layout/partial_closed_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ViewStub
android:id="@+id/bill_partial_open"
layout="@layout/partial_open_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"/>

</LinearLayout>

上面的 View 在另一个名为 bill_view 的 View 中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<br.com.leonardo2204.nubanktest.ui.custom.HeaderView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/list_header_padding">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="@dimen/list_header_padding"
android:paddingStart="@dimen/list_header_padding"
android:text="De 5 Mar até 5 abr"
android:textAllCaps="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingEnd="@dimen/list_header_padding"
android:paddingRight="@dimen/list_header_padding"
android:text="Valores em R$"
android:textAllCaps="true" />

</RelativeLayout>

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

上面的 View 在 PagerAdapter 中膨胀,如下所示:

@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = LayoutInflater.from(container.getContext()).inflate(R.layout.bill_view, container, false);
HeaderView header = (HeaderView) v.findViewById(R.id.header_view);
header.setBill(billList.get(position));
container.addView(v);
return v;
}

问题是,我得到的堆栈跟踪如下:

   Process: br.com.leonardo2204.nubanktest, PID: 10815
java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource
at android.view.ViewStub.inflate(ViewStub.java:289)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setClosedState(HeaderView.java:112)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setupUI(HeaderView.java:91)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.notifyMustRebind(HeaderView.java:76)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setBill(HeaderView.java:72)
at br.com.leonardo2204.nubanktest.ui.adapter.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:30)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17547)

我做错了什么?有没有比 instantiateView 内部更好的方法来膨胀标题 View ?

谢谢!

最佳答案

使用

android:layout="@layout/xyz"

代替

layout="@layout/xyz" 

关于android - 自定义 View 中的 ViewStub 返回 'ViewStub must have a valid layoutResource',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com