当前位置: 首页 > 知识库问答 >
问题:

初始化滚动条未定义?

夏飞鹏
2023-03-14

我正在使用这个库,对于它的一个类(从ViewGroup扩展而来),在“PLA\u AbsListView.java”中,在CTOR中,有以下几行代码:

    final TypedArray a = context.obtainStyledAttributes(R.styleable.View);
    initializeScrollbars(a);
    a.recycle();

最近,我更新了SDK

自从我更新了所有内容后,我一直收到这个错误:

PLA\u AbsListView类型的方法initializeScrollbars(TypedArray)未定义

我尝试将API设置为低于21,但没有帮助。

我还试图找出这个函数的声明位置。它应该是“View.java”中受保护的函数,但由于某种原因,我在文档中看不到它

怎么会呢?

我怎样才能修复它?

是否可能是文档中的错误?

以前,当我们瞄准Kitkat时。。。

共有2个答案

倪阳飇
2023-03-14

View.javaandroid-21来源:

/**
 * ...
 *
 * @removed
 */
protected void initializeScrollbars(TypedArray a) {
    // It's not safe to use this method from apps. The parameter 'a' must have been obtained
    // using the View filter array which is not available to the SDK. As such, internal
    // framework usage now uses initializeScrollbarsInternal and we grab a default
    // TypedArray with the right filter instead here.
    TypedArray arr = mContext.obtainStyledAttributes(com.android.internal.R.styleable.View);

    initializeScrollbarsInternal(arr);

    // We ignored the method parameter. Recycle the one we actually did use.
    arr.recycle();
}

/**
 * ...
 *
 * @hide
 */
protected void initializeScrollbarsInternal(TypedArray a) {

您看不到它,因为该方法被注释为已删除<代码>initializeScrollbarsInternal()也不能使用,因为它带有隐藏注释。从评论来看,使用这种方法不安全,您应该明确地将其报告给lib的作者。

卢英叡
2023-03-14

正如@biegleux在他的回答中提到的那样,初始化滚动条()现在在API 21源代码中用@删除注释。这是来自API 21的方法源:

protected void initializeScrollbars(TypedArray a) {
    // It's not safe to use this method from apps. The parameter 'a' must have been obtained 
    // using the View filter array which is not available to the SDK. As such, internal 
    // framework usage now uses initializeScrollbarsInternal and we grab a default 
    // TypedArray with the right filter instead here. 
    TypedArray arr = mContext.obtainStyledAttributes(com.android.internal.R.styleable.View);

    initializeScrollbarsInternal(arr);

    // We ignored the method parameter. Recycle the one we actually did use. 
    arr.recycle();
}

根据该方法中的注释,听起来API 21之前的问题是传入TypeArray不安全,但现在它不再使用传入的TypeArray。所以看起来这应该用@Deprecated而不是@拿下来注释,并且应该有一个新版本的此方法,当我们需要从以编程方式创建的自定义视图初始化滚动条时,它不需要调用任何参数。

在解决此问题之前,有两种方法可以解决此问题:

1) 使用android:scrollbars属性集从xml中扩展自定义视图。这是最安全的方法,应该适用于所有过去和将来的平台版本。例如:

创建xml布局文件(“my\u custom\u view.xml”):

<com.example.MyCustomView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="horizontal|vertical"/>

为自定义视图充气:

MyCustomView view = (MyCustomView) LayoutInflater.from(context).inflate(R.layout.my_custom_view, container, false);

2) 使用反射在自定义视图的构造函数中调用initializeScrollbars()。如果方法initializeScrollbars()实际被删除或重命名,则在未来的API版本中,此操作可能会失败。例如:

在自定义视图中(例如MyCustomView.java):

public MyCustomView(Context context) {
    super(context);

    // Need to manually call initializedScrollbars() if instantiating view programmatically
    final TypedArray a = context.getTheme().obtainStyledAttributes(new int[0]);
    try {
        // initializeScrollbars(TypedArray)
        Method initializeScrollbars = android.view.View.class.getDeclaredMethod("initializeScrollbars", TypedArray.class);
        initializeScrollbars.invoke(this, a);
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        e.printStackTrace();
    }
    a.recycle();
}
 类似资料:
  • 问题内容: 我正在使用Python3.4,并且尝试安装Fuzzy模块 由于提到了它仅适用于Python2,因此我尝试使用cython对其进行转换。这些是我遵循的步骤: cython Fuzzy.pyx gcc -g -02 -fpic -c Fuzzy.c -o Fuzzy.o 对double_metaphone.c做相同的操作 gcc -shared -o Fuzzy.so Fuzzy.o do

  • 以下是我的配置 java版本“1.8.0_101”java(TM)SE运行时环境(构建1.8.0_101-b13)java热点(TM)64位服务器虚拟机(构建25.101-b13,混合模式) 使用Apache http客户端v4.4调用Restful服务,服务URL具有有效的证书(SHA2) 我们使用apache http客户端调用服务。下面是代码 服务调用是间歇性失败的,有一个批处理过程在一个循

  • 我正在尝试运行一个Spring项目。pom.xml: 结果是这样的: 2017-08-17 01:11:01.405信息9156---[restartedMain]org.ocp.TestruleemallApplication:在桌面上启动TestruleemallApplication-PL25CTR,PID 9156(C:\Users\Ilias\DesktoP\TestRuleEmall\

  • 这对我管用!日志输出: null

  • 问题内容: 我尝试编译,以便通过python导入它。对于建筑,我用过。 构建它之后,我尝试导入,但是发生以下错误。我怎么解决这个问题 ? 错误 fizzbuzz.c setup.py 问题答案: Python不支持,也不支持将任意C文件作为模块。您必须遵循某些约定才能让Python知道您的模块支持哪些功能。 为此,Python将寻找一个函数,模块名称在哪里。Python正在寻找但未能找到它,因此加

  • 可让PS Vita系统软件的设定回复初始设定,使主画面及开始画面皆回到至购买时的状态。 轻触[格式化]>[初始化设定]。请遵循画面指示正确操作。 重要 初始化设定后无法复原,请注意。 初始化中请勿关闭PS Vita的电源。初始化若遭到中断,可能会导致故障。 即使进行此操作,也无法让系统软件回到先前版本。