首页 > 其他分享 >android.content.res.Resources$NotFoundException: String resource ID #0x1

android.content.res.Resources$NotFoundException: String resource ID #0x1

时间:2023-11-22 18:55:43浏览次数:38  
标签:return String res data text resource android type

在Android开发中如果出现android.content.res.Resources$NotFoundException: String resource ID #0x1这样的错误,你想也不用想,一定是Textview控件显示数据出了问题:mTextview.setText(这里的传入的数据一定写成int类型了)。我们需要做的是eg:mTextview.setText(1+""),也就是参数转化成字符串,接下来我们看一源码:

 
如果控件TextView添加的数据写成 eg:mTextview.setText(1)时,也就是传入的是数字,源码分析如下:
 

 

/**
 * Sets the text to be displayed using a string resource identifier.
 *
 * @param resid the resource identifier of the string resource to be displayed
 *
 * @see #setText(CharSequence)
 *
 * @attr ref android.R.styleable#TextView_text
 */
@android.view.RemotableViewMethod
public final void setText(@StringRes int resid) {
    setText(getContext().getResources().getText(resid));
    mTextFromResource = true;
}

 

从该方法的注释我们可以看到参数 resid要是一个string的类型。接着我们往下看getText():

 

 

/**
 * Return the string value associated with a particular resource ID.  The
 * returned object will be a String if this is a plain string; it will be
 * some other type of CharSequence if it is styled.
 * {@more}
 *
 * @param id The desired resource identifier, as generated by the aapt
 *           tool. This integer encodes the package, type, and resource
 *           entry. The value 0 is an invalid identifier.
 *
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 *
 * @return CharSequence The string data associated with the resource, plus
 *         possibly styled text information.
 */
@NonNull public CharSequence getText(@StringRes int id) throws NotFoundException {
    CharSequence res = mResourcesImpl.getAssets().getResourceText(id);
    if (res != null) {
        return res;
    }
    throw new NotFoundException("String resource ID #0x"
            + Integer.toHexString(id));
}

 

上面的意思大概是:返回与特定资源ID相关联的字符串值。返回的对象将是一个字符串,如果这是一个普通的字符串;@param id :所需的资源标识符,由aapt工具生成。这个整数编码了包、类型和资源条目,值0是无效标识符。接着继续往下看类AssetManager:

 

 


 




/**
 * Retrieves the string value associated with a particular resource
 * identifier for the current configuration.
 *检索与特定资源相关联的字符串值当前配置的标识符。
 * @param resId the resource identifier to load
 * @return the string value, or {@code null}
 */
@Nullable
final CharSequence getResourceText(@StringRes int resId) {
    synchronized (this) {
        final TypedValue outValue = mValue;
        if (getResourceValue(resId, 0, outValue, true)) {
            return outValue.coerceToString();
        }
        return null;
    }
}

返回字符序列CharSequence。那么是如何 检索与特定资源相关联的字符串 值。

继续看源码:





/**
 * Populates {@code outValue} with the data associated a particular
 * resource identifier for the current configuration.
 *
 * @param resId the resource identifier to load
 * @param densityDpi the density bucket for which to load the resource
 * @param outValue the typed value in which to put the data
 * @param resolveRefs {@code true} to resolve references, {@code false}
 *                    to leave them unresolved
 * @return {@code true} if the data was loaded into {@code outValue},
 *         {@code false} otherwise
 */
final boolean getResourceValue(@AnyRes int resId, int densityDpi, @NonNull TypedValue outValue,
        boolean resolveRefs) {
    synchronized (this) {
        final int block = loadResourceValue(resId, (short) densityDpi, outValue, resolveRefs);
        if (block < 0) {
            return false;
        }

        // Convert the changing configurations flags populated by native code.
        outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava(
                outValue.changingConfigurations);

        if (outValue.type == TypedValue.TYPE_STRING) {
            outValue.string = mStringBlocks[block].get(outValue.data);
        }
        return true;
    }
}

/** Returns true if the resource was found, filling in mRetStringBlock and
 *  mRetData. */
private native final int loadResourceValue(int ident, short density, TypedValue outValue,
        boolean resolve);

如果找到对应的资源,则返回true。在这里我们也看到了native方法,

肯定是c/c++去做事了,我们不去考虑。如果找不到返回null,

我们从如下代码就可以知道:



@Nullable
final CharSequence getResourceText(@StringRes int resId) {
    synchronized (this) {
        final TypedValue outValue = mValue;
        if (getResourceValue(resId, 0, outValue, true)) {
            return outValue.coerceToString();
        }
        return null;
    }
}


如果找到对应的资源,我们就经过一系列的方法转化成字符序列,

代码如下:


/**
 * Regardless of the actual type of the value, 
try to convert it to a
 * string value.  For example, a color type will be converted 
to a
 * string of the form #aarrggbb.
 * 
 * @return CharSequence The coerced string value.  
If the value is
 *         null or the type is not known, null is returned.
 */
public final CharSequence coerceToString()
{
    int t = type;
    if (t == TYPE_STRING) {
        return string;
    }
    return coerceToString(t, data);
}
    2.------------------------------------------

/**
 * Perform type conversion as per {@link #coerceToString()} on an
 * explicitly supplied type and data.
 * 
 * @param type The data type identifier.
 * @param data The data value.
 * 
 * @return String The coerced string value.  If the value is
 *         null or the type is not known, null is returned.
 */
public static final String coerceToString(int type, int data)
{
    switch (type) {
    case TYPE_NULL:
        return null;
    case TYPE_REFERENCE:
        return "@" + data;
    case TYPE_ATTRIBUTE:
        return "?" + data;
    case TYPE_FLOAT:
        return Float.toString(Float.intBitsToFloat(data));
    case TYPE_DIMENSION:
        return Float.toString(complexToFloat(data)) + DIMENSION_UNIT_STRS[
            (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK];
    case TYPE_FRACTION:
        return Float.toString(complexToFloat(data)*100) + FRACTION_UNIT_STRS[
            (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK];
    case TYPE_INT_HEX:
        return "0x" + Integer.toHexString(data);
    case TYPE_INT_BOOLEAN:
        return data != 0 ? "true" : "false";
    }

    if (type >= TYPE_FIRST_COLOR_INT && type <= TYPE_LAST_COLOR_INT) {
        return "#" + Integer.toHexString(data);
    } else if (type >= TYPE_FIRST_INT && type <= TYPE_LAST_INT) {
        return Integer.toString(data);
    }

    return null;
}
...........................


 

如果控件TextView添加的数据写成 eg:mTextview.setText(1+"")时,也就是传入的是字符串,源码分析如下:
  next1.
/**
 * Sets the text to be displayed. TextView <em>does not</em> accept
 * HTML-like formatting, which you can do with text strings in XML resource files.
 * To style your strings, attach android.text.style.* objects to a
 * {@link android.text.SpannableString}, or see the
 * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources">
 * Available Resource Types</a> documentation for an example of setting
 * formatted text in the XML resource file.
 * <p/>
 * When required, TextView will use {@link android.text.Spannable.Factory} to create final or
 * intermediate {@link Spannable Spannables}. Likewise it will use
 * {@link android.text.Editable.Factory} to create final or intermediate
 * {@link Editable Editables}.
 *
 * @param text text to be displayed
 *
 * @attr ref android.R.styleable#TextView_text
 */
@android.view.RemotableViewMethod
public final void setText(CharSequence text) {
    setText(text, mBufferType);
}

 

next2.

设置要显示的文本

 

/**
 * Sets the text to be displayed and the {@link android.widget.TextView.BufferType}.
 * <p/>
 * When required, TextView will use {@link android.text.Spannable.Factory} to create final or
 * intermediate {@link Spannable Spannables}. Likewise it will use
 * {@link android.text.Editable.Factory} to create final or intermediate
 * {@link Editable Editables}.
 *
 * @param text text to be displayed
 * @param type a {@link android.widget.TextView.BufferType} which defines whether the text is
 *              stored as a static text, styleable/spannable text, or editable text
 *
 * @see #setText(CharSequence)
 * @see android.widget.TextView.BufferType
 * @see #setSpannableFactory(Spannable.Factory)
 * @see #setEditableFactory(Editable.Factory)
 *
 * @attr ref android.R.styleable#TextView_text
 * @attr ref android.R.styleable#TextView_bufferType
 */
public void setText(CharSequence text, BufferType type) {
    setText(text, type, true, 0);

    if (mCharWrapper != null) {
        mCharWrapper.mChars = null;
    }
}
next3.

 

 

private void setText(CharSequence text, BufferType type,
                     boolean notifyBefore, int oldlen) {
    mTextFromResource = false;
    if (text == null) {
        text = "";
    }

    // If suggestions are not enabled, remove the suggestion spans from the text
    if (!isSuggestionsEnabled()) {
        text = removeSuggestionSpans(text);
    }

    ............

    .................

    .........

 

 

notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);

if (needEditableForNotification) {
    sendAfterTextChanged((Editable) text);
} else {
    // Always notify AutoFillManager - it will return right away if autofill is disabled.
    notifyAutoFillManagerAfterTextChangedIfNeeded();
}

// SelectionModifierCursorController depends on textCanBeSelected, which depends on text
if (mEditor != null) mEditor.prepareCursorControllers();

标签:return,String,res,data,text,resource,android,type
From: https://www.cnblogs.com/xzylcf/p/17850059.html

相关文章

  • 无涯教程-Tcl - 字符串(Strings)
    Tcl的原始数据类型是字符串,这些字符串可以包含字母数字字符,仅数字,布尔值甚至二进制数据,Tcl使用16位Unicode字符,字母数字字符可以包含字母,包括非拉丁字符,数字或标点符号。字符串表示与其他语言不同,在Tcl中,当它只是一个单词时,不需要双引号。一个例子可以是-#!/usr/bin/tclshse......
  • Linux系统奇安信浏览器报错跨域:the resource is in more-private address space 'loca
     报错:AccesstoXMLHttpRequestat"123"fromorigin"456"hasbeenblockedbyCORSpolicy:therequestclientisnotasecurecontextandtheresourceisinmore-privateaddressspace'local' 在kylin系统中,升级奇安信浏览器到最新版,会导致以上跨域......
  • 【Spring】SpringCloudの環境構築(restTemplate+ribbon)
    参考URL:<https://zhuanlan.zhihu.com/p/272663162?utm_id=0>■紹介 SpringCloudの初心者に向け、簡単な手順を作成する。Eurekaサーバを利用して、「server」を立って。それにして、提供者(provider)と消費者(consumer)を全て「server」に導入して、管理する。消費者(consumer......
  • wireshark抓包新手使用教程
     Wireshark是非常流行的网络封包分析软件,可以截取各种网络数据包,并显示数据包详细信息。常用于开发测试过程各种问题定位。本文主要内容包括: 1、Wireshark软件下载和安装以及Wireshark主界面介绍。 2、WireShark简单抓包示例。通过该例子学会怎么抓包以及如何简单查看分析......
  • ModuleNotFoundError: No module named 'pip._vendor.progress'
    出现异常:root@linaro-alip:/data/FantClient#./venv_py39_rk/bin/python3-mpipinstallqrcode-ihttps://mirrors.aliyun.com/pypi/simpleTraceback(mostrecentcalllast):File"/usr/lib/python3.9/runpy.py",line197,in_run_module_as_mainre......
  • 实例讲解C++连接各种数据库,包含SQL Server、MySQL、Oracle、ACCESS、SQLite 和 Postgr
     C++是一种通用的编程语言,可以使用不同的库和驱动程序来连接各种数据库。以下是一些示例代码,演示如何使用C++连接SQLServer、MySQL、Oracle、ACCESS、SQLite和PostgreSQL、MongoDB数据库。连接SQLServer数据库要使用C++连接SQLServer数据库,可以使用Micro......
  • Misc_BUUCTF_WriteUp | wireshark
    题目提示:黑客通过wireshark抓到管理员登陆网站的一段流量包(管理员的密码即是答案)注意:得到的flag请包上flag{}提交题目:(pcap文件分析根据提示,我们需要找到管理员的密码。一般密码这类机密性高的信息会通过POST请求提交数据,于是打开文件,过滤出POST请求试试:过滤......
  • 【Java基础】String类 && StringBuilder类
    String类String类特点字符串底层是字节类型的数组:byte[]Java程序中所有双引号字符串,都是String类的实例(对象)字符串在创建之后,其内容不可更改字符串虽然不可以改变,但是可以被共享(通过字符串常量池)字符串常量池(StringTable)-->当使用双引号创建字符串对象时,会检查常量池中是......
  • Java字符串分割[split()]和截取[substring()]
    字符串的分割:一般自字符串的分割常用的方法是java.lang包中的String.split()方法,返回是一个字符串数组。语法:publicString[]split(Stringregex,intlimit)参数:regex -- 正则表达式分隔符。limit --分割的份数。比如:需要分割字符串中的每个字符(空格也会被看做字符),split()中......
  • Wireshark抓包分析TCP三次握手
    TCP握手过程分析TCP三次握手示意图  第一次握手:客户端向服务器发送一个SYN段(表示发起连接请求),并且包含客户端的一个初始序列号seq=0第二次握手:服务端返回一个ACK(对客户端连接请求的应答)+SYN(表示服务端发起连接请求),并且包含服务端的一个初始序列号seq=0,同时返回一个确......