今天在写安卓的时候,使用ViewPager,每一个页面都使用到ListView,但是突然又不想写Adapter,然后就想自己添加几个TextView进去测试测试。
谁知道,调用了AddView方法后报错了。
添加的代码如下:
<span style="white-space:pre"> </span>@Override public Object instantiateItem(View view, int position) // 实例化Item { Toast.makeText(mContext, "ListView"+position, 1000).show(); ((ViewPager) view).addView(mViewList.get(position), 0); ListView listView = mViewList.get(position); TextView txt = new TextView(mContext); txt.setText("ViewPager "+ (position+1)+" "); txt.setTextSize(20); txt.setGravity(Gravity.CENTER); listView.addView(txt, 0); return listView; }
报的错误如下:
02-05 01:24:44.746: E/AndroidRuntime(3489): FATAL EXCEPTION: main 02-05 01:24:44.746: E/AndroidRuntime(3489): Process: com.todayfocus, PID: 3489 02-05 01:24:44.746: E/AndroidRuntime(3489): java.lang.UnsupportedOperationException: addView(View, int) is not supported in AdapterView 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.AdapterView.addView(AdapterView.java:465) 02-05 01:24:44.746: E/AndroidRuntime(3489): at com.todayfocus.adapter.CommunityPagerAdapter.instantiateItem(CommunityPagerAdapter.java:58) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.support.v4.view.ViewPager.populate(ViewPager.java:1020) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.support.v4.view.ViewPager.populate(ViewPager.java:952) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at com.special.ResideMenu.TouchDisableView.onMeasure(TouchDisableView.java:50) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 02-05 01:24:44.746: E/AndroidRuntime(3489): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.View.measure(View.java:16628) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5622) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.Choreographer.doCallbacks(Choreographer.java:574) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.Choreographer.doFrame(Choreographer.java:544) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.os.Handler.handleCallback(Handler.java:733) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.os.Handler.dispatchMessage(Handler.java:95) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.os.Looper.loop(Looper.java:212) 02-05 01:24:44.746: E/AndroidRuntime(3489): at android.app.ActivityThread.main(ActivityThread.java:5135) 02-05 01:24:44.746: E/AndroidRuntime(3489): at java.lang.reflect.Method.invokeNative(Native Method) 02-05 01:24:44.746: E/AndroidRuntime(3489): at java.lang.reflect.Method.invoke(Method.java:515) 02-05 01:24:44.746: E/AndroidRuntime(3489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877) 02-05 01:24:44.746: E/AndroidRuntime(3489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 02-05 01:24:44.746: E/AndroidRuntime(3489): at dalvik.system.NativeStart.main(Native Method)
大概的意思就是,继承了AdapterView的控件是不支持AddView()方法的。所以说,还是要自己写Adapter类才行,实在是太苦逼了!
于是乎我就继续写Adapter了。