从android中的自定义组件以编程方式创建滚动视图
我正在尝试在 Android 中构建一个复合控件,其中包含(除其他外)ScrollView。当我尝试在 Eclipse 中查看控件时,出现问题,在错误消息“Parser 不是 BridgeXmlBlockParser”之后出现 NullPointerException 崩溃。
Stacktrace:
java.lang.NullPointerException
at android.view.View.<init>(View.java:1720)
at android.view.ViewGroup.<init>(ViewGroup.java:277)
at android.widget.FrameLayout.<init>(FrameLayout.java:83)
at android.widget.ScrollView.<init>(ScrollView.java:128)
at android.widget.ScrollView.<init>(ScrollView.java:124)
at android.widget.ScrollView.<init>(ScrollView.java:120)
at my.compound.control.StringPicker.onMeasure(StringPicker.java:46)
...
我已将错误跟踪到以下条件:
- 抛出 NPE,因为当
attrs
Context.obtainStyledAttributes() 调用返回null
> 传递的参数为null
。 - 这仅适用于 Eclipse 中使用的
BridgeContext
实现,它期望attrs
是BridgeXmlBlockParser
的实例。 attrs
参数为null
因为我使用 (Context) 构造函数创建 ScrollView。
当然有一个解决方法,即传递 Eclipse 构造复合控件时收到的 attrs
,但我不希望在复合控件上设置的所有属性都应用于我的内部控件。
我做错了什么吗?这是 Android Eclipse 中的一个错误吗?...?
这就是 my.compound.control.StringPicker.onMeasure 的样子(为了清楚起见,稍微删除了它):
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (this.getChildCount() != requestedLength) {
this.removeAllViews();
int childWidth = getWidth() / requestedLength;
int childHeight = getHeight();
for (int i = 0; i < requestedLength; i++) {
ScrollView child = new ScrollView(getContext()); // NPE here
child.setLayoutParams(new LayoutParams(childWidth, childHeight));
addView(child);
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
I'm trying to build a compound control in Android, containing (among other things) a ScrollView. Things go wrong when I try to view the control in Eclipse, crashing with a NullPointerException after the error message: "Parser is not a BridgeXmlBlockParser".
Stacktrace:
java.lang.NullPointerException
at android.view.View.<init>(View.java:1720)
at android.view.ViewGroup.<init>(ViewGroup.java:277)
at android.widget.FrameLayout.<init>(FrameLayout.java:83)
at android.widget.ScrollView.<init>(ScrollView.java:128)
at android.widget.ScrollView.<init>(ScrollView.java:124)
at android.widget.ScrollView.<init>(ScrollView.java:120)
at my.compound.control.StringPicker.onMeasure(StringPicker.java:46)
...
I've traced the error to the following conditions:
- The NPE is thrown because a
Context.obtainStyledAttributes()
call returnsnull
when theattrs
argument passed isnull
. - This only applies to the
BridgeContext
implementation used in Eclipse, which expectsattrs
to be an instance of theBridgeXmlBlockParser
. - The
attrs
argument isnull
because I create the ScrollView using the (Context) constructor.
There is a workaround of course, which is passing the attrs
I receive when Eclipse constructs the compound control, but I don't want all the attributes set on the compound control to apply to my inner control.
Am I doing something wrong, is this a bug in Android Eclipse, ...?
This is what my.compound.control.StringPicker.onMeasure looks like (stripped it a bit for clarity):
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (this.getChildCount() != requestedLength) {
this.removeAllViews();
int childWidth = getWidth() / requestedLength;
int childHeight = getHeight();
for (int i = 0; i < requestedLength; i++) {
ScrollView child = new ScrollView(getContext()); // NPE here
child.setLayoutParams(new LayoutParams(childWidth, childHeight));
addView(child);
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何通过 XML 布局或在代码中动态创建复合控件?
我能想到的一个可能的原因是您通过 XML 添加它,但您可能没有添加 StringPicker(Context context, AttributeSet attrs) 构造函数。在那里你应该调用 super(context, attrs)。
How are you creating your compound control, via XML layout or dynamically in the code?
A possible reason I could think of is that you are adding it via XML but you may not have added the StringPicker(Context context, AttributeSet attrs) constructor. There you should call super(context, attrs).
这似乎是旧版 Android 版本中的一个错误。
该问题在 Android 2.3 或更高版本中不会出现,但在选择 Android 2.2 或更低版本时会出现。这些旧 Android 版本的解决方法是(如问题中所述)从构造函数复制
attrs
参数。仅当您想在 Eclipse 中与这些旧版本一起使用设计视图时才需要这样做,要在旧版本中运行应用程序,不需要任何解决方法。
It seems to have been a bug in older Android versions.
The problem does not appear in Android version 2.3 or higher, but does appear when selecting Android 2.2 or lower. The workaround for these older Android versions is (as mentioned in the question) to copy the
attrs
parameter from the constructor.This is only needed if you want to use design-view in Eclipse with these older versions, to run your application in the older versions no workaround is necessary.