convertView保持为null

时间:2014-11-25 18:33:59

标签: android baseadapter unsupportedoperation

我得到了一个有趣的"因为我在getView方法中在BaseAdapter中扩展我的布局而且我将返回的视图转换为convertView。有趣的事情是,convertView保持为null:

convertView = myInflater.inflate(R.layout.customadapter_view_layout_medium, parent);

我通过调试:

enter image description here

一切都得到了正确的参考。 Parent引用GridView,myInflater不为null,我膨胀的布局的id也存在。但是convertView保持为空。

这是我的logCat:

11-25 18:29:38.711: E/AndroidRuntime(2377): FATAL EXCEPTION: main
11-25 18:29:38.711: E/AndroidRuntime(2377): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.AdapterView.addView(AdapterView.java:477)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at com.android.faccess.NewCustomAdapter.getView(NewCustomAdapter.java:87)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.GridView.onMeasure(GridView.java:1040)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.View.measure(View.java:15513)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.View.measure(View.java:15513)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.View.measure(View.java:15513)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.View.measure(View.java:15513)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.View.measure(View.java:15513)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.Choreographer.doFrame(Choreographer.java:532)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.os.Handler.handleCallback(Handler.java:725)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.os.Looper.loop(Looper.java:137)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at android.app.ActivityThread.main(ActivityThread.java:5039)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at java.lang.reflect.Method.invoke(Method.java:511)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-25 18:29:38.711: E/AndroidRuntime(2377):     at dalvik.system.NativeStart.main(Native Method)

任何人都可以告诉我为什么这行抛出UnsupportedOperationException?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

如果您提供父级,则需要提供false的true作为第3个参数,或者您要在父级上注入膨胀的视图。此断点上的convertview也为null。它只会在该行之后有一个值。但该行正在抛出异常,因为您错过了最后一个参数。

convertView = myInflater.inflate(R.layout.customadapter_view_layout_medium, parent,false);