2

我正在学习 Google Android App Course,但遇到了一个奇怪的错误。如果我调用initVariables(),则不会出现布局,如果我将其注释掉,则会出现。如果我在调用后返回getResources(),我会看到布局,如果我在调用后返回getString(),则没有布局。

在 logcat 中,我只看到达到了一些空闲超时。我目前的环境存在一些问题(eclipse 崩溃等),所以我无法发布完整的 logcat。这是代码和xml:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.advanced);
    initLayout();
    initVariables();
}

protected void initLayout() {
    m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
    m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
    m_vwJokeLayout = (LinearLayout) findViewById(R.id.jokeListLayout);
}

protected void initVariables() {
    Resources resources = getResources();
    author = resources.getString(R.string.author_name);
    m_nDarkColor = resources.getColor(R.color.dark);
    m_nLightColor = resources.getColor(R.color.light);
    m_arrJokeList = new ArrayList<Joke>();
}

布局

<?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/InputLayoutViewGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/addJokeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Joke" />

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

    <ScrollView
        android:id="@+id/jokeListViewGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/jokeListLayout"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>
4

1 回答 1

0

如果在运行时未找到 R.string.author_name,我将抛出异常。你可以捕捉到异常

    try {
        author = resources.getString(R.string.author_name);
    } catch (Resources.NotFoundException e) {

    }

但问题应该是为什么你的 R.string.author_name 不见了?你能成功获得任何字符串资源吗?

于 2013-03-11T16:13:35.837 回答