gpt4 book ai didi

java - 如何以编程方式将 View 添加到 ViewContainer?

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:29 25 4
gpt4 key购买 nike

当我重写 MainAdapter extends PagerAdapterpublic Object instantiateItem 函数时,在 container.addView( 行,它崩溃了。我收到以下日志

java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3909)
at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3890)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1302)
at android.view.ViewGroup.addView(ViewGroup.java:3744)
at com.jfeinstein.jazzyviewpager.JazzyViewPager.addView(JazzyViewPager.java:127)
at com.ycg.bottommenuslidegradientswipe.MainActivity$MainAdapter.instantiateItem(MainActivity.java:260)

我的instantiateItem函数的代码如下所示。类 TaskCompletedView 扩展 View

@Override
public Object instantiateItem(ViewGroup container, final int position) {
TasksCompletedView mTasksView = (TasksCompletedView) findViewById(R.id.tasks_view);
container.addView(mTasksView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
jazzyPager.setObjectForPosition(mTasksView,position);

new Thread(new ProgressRunable()).start();
return mTasksView;
}

最佳答案

发生这种情况是因为您设置了空变量。mTasksView == null;您需要放置非空 View ,例如:

container.addView(new ImageView(context), LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

关于java - 如何以编程方式将 View 添加到 ViewContainer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28813790/

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