Android:如何在库中正确加载 ViewStub [英] Android: How to load correctly ViewStub in library

查看:25
本文介绍了Android:如何在库中正确加载 ViewStub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用 ViewStub 的库并将其集成到我的项目中.当我运行应用程序时,我得到 java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource 并且在 Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' 之后在空对象引用上.

I created a library that uses ViewStub and integrated it to my project. When I run the application I get java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource and just after Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference.

我创建了另一个没有该库的项目,但使用了完全相同的视图存根,并且可以正常工作.

I created another project without that library but the exact same viewstub and it worked.

所以我猜库加载 ViewStub 的速度比应用程序慢.如果我错了,请纠正我.

So I guess the library loads the ViewStub slower the the application. Correct me if I'm wrong.

有什么方法/解决方案可以从库中加载 ViewStub 吗?

Is there any approach/solution to load the ViewStub from the library ?

推荐答案

所以我想回答我的问题给任何一个将来会遇到这个问题的人.

So I want to answer to my question to any one will face this problem in the future.

我只是用 替换了标签 并像这样添加了布局

I just replaced the tag <ViewStub> by <include> and added the layout to it like this

<include
    android:id="@+id/include_action_bar"
    android:layout_width="fill_parent"
    android:layout_height="59dp"
    layout="@layout/actionbar_layout_sml" />

然后我根据我的需要设置可见性(例如对 actionBar):

and then I setup the visibility depending of my needs like this (for example to the actionBar):

public void setActionBarVisibility(boolean visible) {
    View view = findViewById(R.id.include_action_bar);
    if (view != null) {
        if (visible) {
            view.setVisibility(View.VISIBLE);
        }
        else {
            view.setVisibility(View.GONE);
        }
    }
}

希望这对某个地方的某人有所帮助.

Hope this will help someone, somewhere.

这篇关于Android:如何在库中正确加载 ViewStub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆