神秘(?)的XML布局错误 [英] Mysterious(?) error in XML layout

查看:115
本文介绍了神秘(?)的XML布局错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个非常简单的列表布局,在此之后发现CommonsWare例子: HTTP ://www.commonsware.com/Android/excerpt.pdf 。以下是我的相关文件:

I'm trying to create a very simple list layout, following the CommonsWare examples found here: http://www.commonsware.com/Android/excerpt.pdf. Here are my relevant files:

RateListDemo.java

RateListDemo.java

package my.ratelist;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class RateListDemo extends ListActivity {

private static final String[] items={"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing",
    "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat",
    "ante", "porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.label, items));
    setContentView(R.layout.list);
}
}

list.xml

list.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">

<ListView android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

row.xml

row.xml

<?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="wrap_content"
android:orientation="horizontal" >

<RatingBar
    android:id="@+id/rate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="3" />

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

因此​​,这里是真正奇怪的事情。这code运行正常。但是,如果我从row.xml删除的RatingBar项目,只留下TextView的项目,然后根据运行抛出致命错误。这是没有意义的我,特别是因为我有一个运行没有问题,一个非常类似的布局方案另一个应用程序。正如你所看到的,有一个在code到的RatingBar项目别处没有提及。另外,如果我在切换的row.xml和的RatingBar TextView的项目的顺序,它抛出了同样的错误。这是怎么回事?

So here's the really weird thing. This code runs fine. However, if I remove the RatingBar item from row.xml and leave just the TextView item, then it throws fatal errors upon running. This makes no sense to me, especially since I have another application that runs a very similar layout scheme with no issue. As you can see, there's no reference elsewhere in the code to the RatingBar item. Also, if I switch the order of the RatingBar and TextView items in row.xml, it throws the same error. What's the deal?

最初的错误消息是在android.widget.ArrayAdapter.createViewFromResource NullPointerException异常。我可以提供整个日志是否有帮助。

The initial error message is NullPointerException at android.widget.ArrayAdapter.createViewFromResource. I can provide the entire log if that helps.

编辑:

嗯,试图在这些不同的布局配置运行它,并反复得到致命错误后好了,他们现在所有的工作。但我并没有改变,除了尝试运行了几次东西。我觉得已经用XML前发生在我身上。所以,问题依然存在:WTF

Um, well after trying to run it in these different layout configurations and repeatedly getting fatal errors, they all now work. But I didn't change anything except try to run it a few times. I feel like that has happened to me before with XML. So, the question remains: WTF?

推荐答案

至于建议,我再次尝试这一点,得到了同样的致命错误,然后干净的项目。这似乎已经解决了这一问题。

As suggested, I tried this again, got the same fatal error, and then "cleaned" the project. This appears to have fixed the issue.

这篇关于神秘(?)的XML布局错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆