Android 错误获取资源编号值时没有包标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:01:34  来源:igfitidea点击:

ERROR No package identifier when getting value for resource number

androidandroid-layoutandroid-resources

提问by Dean-O

Both activities are in the same package

两个活动在同一个包中

Second activity uses second layout file

第二个活动使用第二个布局文件

setContentView(R.layout.main2);

Errors on this line in the Second_Activity.

Second_Activity 中这一行的错误。

EditText text1 = (EditText) findViewById(R.id.EditText03);

Here is the layout file for the Second_Activity.

这是 Second_Activity 的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Answer Is : " >
        </TextView>

        <EditText
            android:id="@+id/EditText03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Calling an intent" >
    </Button>

</LinearLayout>

Here are the errors in the LogCat window

以下是 LogCat 窗口中的错误

08-01 19:32:20.340: WARN/ResourceType(8875): No package identifier when getting value for resource number 0x00000005
08-01 19:32:20.390: ERROR/AndroidRuntime(8875): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5 

mail.xml

邮件.xml

<TextView 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:text="First Number : ">
</TextView>

<EditText 
    android:id="@+id/EditText01" 
    android:inputType="number"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</EditText>

<TextView 
    android:id="@+id/TextView02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:text="Second Number: ">
</TextView>

<EditText 
    android:id="@+id/EditText02" 
    android:inputType="number"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</EditText>

secondscreen.xml

第二屏.xml

<TextView 
    android:id="@+id/TextView03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:text="Answer Is : ">
</TextView>

<EditText 
    android:id="@+id/main2EditText01" 
    android:inputType="number"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</EditText>

manifest xml file

清单 xml 文件

    <activity android:name=".ActivityTwo"/>

回答by John Weidner

I got this same error message when I tried to use TextView.setText passing a char instead of a String. This makes sense since the char would be promoted to an int which meant that I was really calling the

当我尝试使用 TextView.setText 传递一个字符而不是一个字符串时,我收到了同样的错误消息。这是有道理的,因为 char 将被提升为 int,这意味着我真的在调用

TextView.setText( int resId );

And since there wasn't a resource with that value, it wouldn't work.

并且由于没有具有该值的资源,因此它不起作用。

回答by u1201201

face with the same error

面对同样的错误

finally i found its not a error due to your xml layout

最后我发现它不是由于您的 xml 布局导致的错误

somewhere in your code set TextView.setText(int)

在你的代码集中的某个地方 TextView.setText(int)

try TextView.setText( Integer.toString(int));

尝试 TextView.setText( Integer.toString(int));

回答by Henok

When you pass an integer to the TextView.setText()to be displayed android assumes it is a resource id and that's why you are getting Resource$NotFoundException. Try converting the int to String before passing it to TextView.setText(): TextView.setText(String.valueOf(i)).

当您将一个整数传递TextView.setText()给要显示的 android 时,它会假定它是一个资源 ID,这就是您获得Resource$NotFoundException. 在将 int 传递给TextView.setText():之前尝试将其转换为 String TextView.setText(String.valueOf(i))

回答by Paul

Just for the protocol, You could also use:

仅用于协议,您还可以使用:

TextView.setText("" + intVar)instead of TextView.setText(intVar)

TextView.setText("" + intVar)代替 TextView.setText(intVar)

回答by Vignan Nani

I got the same error while trying to print integer value : TextView.setText(int value). I resolved this error by converting the integer value to string and the i used TextView.setText(converted string value)

我在尝试打印整数值时遇到了同样的错误:TextView.setText(int value)。我通过将整数值转换为字符串解决了这个错误,我使用了 TextView.setText(converted string value)

回答by nekiala

I had the same problem before I come to this post. For me, it was like this: view_element.setText( an_int_value). After casting view_element.setText(String.valueOf(an_int_value));, everything is okay.

在我来这篇文章之前,我遇到了同样的问题。对我来说,它是这样的:view_element.setText( an_int_value). 铸造后view_element.setText(String.valueOf(an_int_value));,一切正常。

回答by Rigoberto Torres

From Android TextView Documentation:

来自Android TextView 文档

  • setText(int resid)Sets the text to be displayed using a string resource identifier.
  • setText(int resid)使用字符串资源标识符设置要显示的文本。

回答by Lucy Lu

For me I had to go in the XML file for the button. There I noticed a hard coded string value. I had to remove that, and also I had to use Textview.setText("" + intVar);

对我来说,我必须进入按钮的 XML 文件。在那里我注意到一个硬编码的字符串值。我不得不删除它,而且我不得不使用Textview.setText("" + intVar);

回答by Ankit Gupta

It is due to typecast error. You have to try this- TextView.setText(Integer.toString(variable_name));

这是由于类型转换错误。你必须试试这个- TextView.setText(Integer.toString(variable_name));

Here toString is used to convert integer to string for showing text.

这里 toString 用于将整数转换为字符串以显示文本。

回答by saintjab

I was using Picasso library to load image from the network. The urls are in a ArrayList I wasn't using arraylist.get() to get the position of the url in the ArrayList.

我正在使用毕加索库从网络加载图像。网址在 ArrayList 中,我没有使用 arraylist.get() 来获取网址在 ArrayList 中的位置。