0

我遇到了一个“有趣”的问题,因为我在 getView 方法的 BaseAdapter 中膨胀了我的布局,并将返回的视图提供给 convertView。有趣的是,convertView 保持为空:

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

我经历了调试:

在此处输入图像描述

一切都得到了他的正确参考。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?任何帮助表示赞赏。

4

1 回答 1

0

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

convertView = myInflater.inflate(R.layout.customadapter_view_layout_medium, parent,false);
于 2014-11-25T18:38:49.957 回答