1
當我覆蓋函數MainAdapter extends PagerAdapter
,在行container.addView(
,它崩潰了。我收到以下日誌如何以編程方式將視圖添加到ViewContainer?
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)
我的代碼instanstiateItem
函數如下所示。 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;
}
我不知道在哪裏更新我的代碼。我用'findViewById(R.id.tasks_view)'這行來初始化mTaskView。 'mTaskView',Class'TaskCompletedView'擴展了'View'。 – 2015-04-01 06:24:28
@PeterZhu所以,findViewById返回null,因爲沒有找到該id的視圖。請確認正確的初始化。 – smail2133 2015-04-01 06:53:18