当前位置: 首页 > 知识库问答 >
问题:

在Android中使用资源会冻结主线程

薛博艺
2023-03-14

我正在上谷歌Android应用程序课程,我遇到了一个奇怪的错误。如果调用initvariables(),则不会出现布局,如果注释掉,则会出现布局。如果我在调用getresources()后返回,我将看到布局,如果我在调用getstring()后返回,则没有布局。

@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>

共有1个答案

洪知
2023-03-14

如果在运行时找不到r.string.author_name,我将抛出一个异常。您可以捕捉到异常

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

    }

但问题应该是为什么您的r.string.author_name丢失了?您能够成功地获得任何字符串资源吗?

 类似资料:
  • I am not a number. "The Prisoner" — Number Six 将机器搬来搬去是很常见的做法,尤其是在云的基础设施中搬动,所以一个特定主机的 IP 经常会改变。 正因为如此,在你的配置中使用硬编码的 IP 地址显然是个坏主意。 如果一台机器要访问另一台(例如,一个应用服务器需要访问一台数据库服务器), 那么使用主机名而不是 IP 地址会更好。 然而,如何映射主机名到

  • 我有一个带有“开始”按钮和进度条的小型GUI测试。期望的行为是: 单击开始 Progressbar振荡5秒 进度条停止 观察到的行为是“开始”按钮冻结5秒钟,然后显示进度条(无振荡)。 以下是我目前的代码: 根据Bryan Oakley提供的信息,我知道我需要使用线程。我试着创建一个线程,但我猜,由于线程是从主线程中开始的,所以没有帮助。 我的想法是将逻辑部分放在一个不同的类中,并从该类中实例化G

  • 下表列出了一些方便的URL以及其内容的简短描述。 这些URL中的大多数也列在本书的其他位置,但此表将它们组合在一个独立的位置。 URL Descripton http://freeradius.org FreeRADIUS项目的主页。 http://wiki.freeradius.org FreeRADIUS项目的Wiki,用作文档的主要来源。 http://wiki.freeradius.org

  • 问题内容: 我有一个带有“开始”按钮和进度条的小型GUI测试。所需的行为是: 点击开始 进度条振荡5秒钟 进度栏停止 观察到的行为是“开始”按钮冻结5秒钟,然后显示进度条(无振荡)。 到目前为止,这是我的代码: 根据Bryan Oakley 在此提供的信息,我了解我需要使用线程。我尝试创建一个线程,但是我猜测由于该线程是从主线程中启动的,因此没有帮助。 我有想法放置在不同的类中的逻辑部分,以及从该

  • 问题内容: 假设我的应用程序的原始资源文件夹中有一个包含JSON内容的文件。如何将其读入应用程序,以便解析JSON? 问题答案: 参见openRawResource。这样的事情应该起作用:

  • 注意:我工作了很多时间并研究了google和stackoverflow,但我找不到答案。 我用线。sleep(),它冻结了所有其他JDialog、JFrame和线程。 我的示例代码: 在这种情况下,JDialog无法正确显示: inccorect出现jdialog 但它必须符合这一点: true出现jdialog 我怎样才能解决这个问题。我想让主线程等待另一个线程。有人可以纠正我的示例代码,或者在