从android中的自定义组件以编程方式创建滚动视图

发布于 2024-09-02 19:44:43 字数 1830 浏览 9 评论 0原文

我正在尝试在 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,因为当 attrsContext.obtainStyledAttributes() 调用返回 null > 传递的参数为null
  • 这仅适用于 Eclipse 中使用的 BridgeContext 实现,它期望 attrsBridgeXmlBlockParser 的实例。
  • 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 returns null when the attrs argument passed is null.
  • This only applies to the BridgeContext implementation used in Eclipse, which expects attrs to be an instance of the BridgeXmlBlockParser.
  • The attrs argument is null 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-09-09 19:44:43

您如何通过 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).

烟雨扶苏 2024-09-09 19:44:43

这似乎是旧版 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文