Java 由于“故障”而导致的崩溃;获取资源号的值时没有包标识符…“;

Java 由于“故障”而导致的崩溃;获取资源号的值时没有包标识符…“;,java,android,xml,debugging,resources,Java,Android,Xml,Debugging,Resources,好的,在搜索了我能找到的关于这个主题的各种线索,并尝试了所有对其他人有用的东西之后。。。我不得不屈从于发布我自己的问题的需要 作为Android和Java的不速之客,你必须原谅像孩子一样跟我说话的必要性:) 到目前为止,我有一个有3个活动的应用程序。用作导航屏幕的主窗口,用于从其他两个窗口中选择一个。第一个很好用,但是当我尝试第二个按钮时。。。它崩溃了 我有一些代码,我正在尝试运行onCreate。。。因此,当我看到导致崩溃的往往是XML布局。。我注释掉了Java代码。。。并且可以很好地预处理布

好的,在搜索了我能找到的关于这个主题的各种线索,并尝试了所有对其他人有用的东西之后。。。我不得不屈从于发布我自己的问题的需要

作为Android和Java的不速之客,你必须原谅像孩子一样跟我说话的必要性:)

到目前为止,我有一个有3个活动的应用程序。用作导航屏幕的主窗口,用于从其他两个窗口中选择一个。第一个很好用,但是当我尝试第二个按钮时。。。它崩溃了

我有一些代码,我正在尝试运行onCreate。。。因此,当我看到导致崩溃的往往是XML布局。。我注释掉了Java代码。。。并且可以很好地预处理布局负载

把代码放回去,它会再次崩溃

在LogCat中,我看到一行写着“获取资源号0x0000000d的值时没有包标识符”

然后是关于致命异常和关闭VM的各种行

因此,为了从其他帖子中获得信心,我去了R.java文件,查看哪些资源具有该ID。。。而且它不在里面。它们都以0x7f开头。。。现在唯一以“d”结尾的是ID为“game_Answer1”的文本视图。但我不确定这是否是它所指的

我尝试了EclipseClean命令,还删除了R.java。。。同样的问题

下面是似乎要崩溃的java代码。。。再一次,请不要嘲笑我的意大利面代码,因为这是Java中超越hello world应用程序的第二次尝试


下面是与此问题相关的XML布局。。。以防我遗漏了一些明显的东西:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#ffffff"  android:id="@+id/gamelayoutwrapper">
<RelativeLayout android:layout_height="wrap_content" android:id="@+id/games_animalnameFrame" android:layout_width="fill_parent" android:gravity="right">
    <TextView android:layout_height="wrap_content" android:id="@+id/game_animalname" android:layout_width="fill_parent" android:textSize="60sp" android:layout_alignParentRight="true" android:gravity="right" android:text="__nteater"></TextView>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:id="@+id/game_animalimageFrame" android:layout_below="@+id/games_animalnameFrame" android:layout_centerInParent="true" android:layout_height="wrap_content" android:gravity="top">
    <ImageView android:id="@+id/game_animalImage" android:src="@drawable/elephant" android:scaleType="fitCenter" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="10dp"></ImageView>
</RelativeLayout>
<RelativeLayout android:layout_height="wrap_content" android:id="@+id/gameLettersFrame" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_centerHorizontal="true" android:background="#cccccc">

            <TextView android:layout_height="wrap_content" android:id="@+id/game_Answer1" android:text="A" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="left" android:clickable="true" android:textSize="100sp" android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:textColor="#090999" android:background="#ccccFF"></TextView>
            <TextView android:layout_height="wrap_content" android:id="@+id/game_Answer2" android:text="B" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="center_horizontal" android:clickable="true" android:textSize="100sp" android:layout_width="wrap_content" android:layout_centerInParent="true" android:textColor="#090999" android:background="#ffcccc"></TextView>
            <TextView android:layout_height="wrap_content" android:id="@+id/game_Answer3" android:text="C" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="right" android:clickable="true" android:textSize="100sp" android:layout_width="wrap_content" android:layout_alignParentLeft="false" android:layout_alignParentRight="true" android:textColor="#090999" android:background="#ccffcc"></TextView>

</RelativeLayout>


在R.java文件中,是否存在gamelayout的id?这应该位于

公共静态最终类布局{
}
在R文件中?这是确定应用程序崩溃原因的第一个重要步骤。

不幸的是,Android不接受带大写字母的资源标识符。 另外,请确保在根视图中定义名称空间

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/game_answer1" 
            android:text="A" 
            android:paddingLeft="20dp" 
            android:paddingRight="20dp" 
            android:layout_gravity="left" 
            android:clickable="true" 
            android:textSize="100sp" 
            android:layout_alignParentLeft="true" 
            android:textColor="#090999" 
            android:background="#ccccFF"
            />             
    </RelativeLayout>

Android不允许使用大写的资源名称

改名

R.id.game_答案1

游戏(答案)(一)


等等对于另一个的,你有

我不久前遇到过同样的问题,我假设它是固定的,但我认为另一个答案总是受欢迎的:)当使用setText()方法时,你不能通过诸如int和double之类的基本数据类型发送,而是转换为字符串,然后通过字符串发送。这解决了我的问题,当它弹出时

您的项目中可能有其他错误,因为您的R.java创建得不好。因此,在执行时,它不会获取具有该id的资源。R.id包含项目中所有资源的id。

现在回答这个问题已经很晚了,不过对于那些通过搜索找到该线程的人来说可能会很有用

setText方法需要标识符作为参数。 因此,values/strings.xml中应该有一个名为“empty_answer”的字符串,包含值“-”。换句话说,该行类似于: -

然后,setText应该像这样调用: g_answera.setText(R.string.empty_answer)

很可能,在您的情况下,字符串“-”以某种方式被转换为资源id 0x0000000d,而这在您的应用程序中当然不存在。虽然很奇怪,这样的代码是编译的


还有一条评论:在Android中,可以用大写字母命名ID等资源。什么是不可能的-在资源文件的名称中包含大写字符。

我想查看LinearLayout的ID名称,以确保我在代码中访问了正确的ID名称。对我来说,发生此错误是因为我忘记将android:id=“@+id/myid”添加到我的一个XML布局文件中(因为该文件当时不是我关注的焦点!)

是的,它确实存在于我的R.java文件中。而且,就像我说的,如果我不引用任何文本视图,那么加载的东西就可以了。事实上,我注释掉了TextView引用,并使用随机整数加载了一个随机图像,这很有效。。。我的文本视图有问题,但我不知所措!那很有趣。但这很奇怪,我有大写字母的资源,例如firstName,这很好用。我猜这可能与下划线分隔符有关?是的,它们是在XML文件中创建的资源id,但资源文件不能有大写文本。您可以检查是否出现此错误,在标准输出控制台中,您会看到类似“[2011-02-11 23:12:09-应用程序名称]XML文件中的错误:中止构建。令人惊讶的是,这解决了我的问题。值得您的销售代表增加50%!这应该被接受为一个答案。
<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/game_answer1" 
            android:text="A" 
            android:paddingLeft="20dp" 
            android:paddingRight="20dp" 
            android:layout_gravity="left" 
            android:clickable="true" 
            android:textSize="100sp" 
            android:layout_alignParentLeft="true" 
            android:textColor="#090999" 
            android:background="#ccccFF"
            />             
    </RelativeLayout>