Java 在Android中使用资源会冻结主线程
我在上谷歌安卓应用程序课程,遇到了一个奇怪的错误。如果我调用Java 在Android中使用资源会冻结主线程,java,android,Java,Android,我在上谷歌安卓应用程序课程,遇到了一个奇怪的错误。如果我调用initVariables(),布局不会出现,如果我注释掉它,它就会出现。如果我在调用getResources()后返回,我将看到布局,如果我在调用getString()后返回,则没有布局 在logcat中,我只看到一些空闲超时。 我目前在我的环境中遇到了一些问题(eclipse崩溃等),所以我无法发布完整的日志。 以下是代码和xml: @Override public void onCreate(Bundle savedInstanc
initVariables()
,布局不会出现,如果我注释掉它,它就会出现。如果我在调用getResources()
后返回,我将看到布局,如果我在调用getString()
后返回,则没有布局
在logcat中,我只看到一些空闲超时。
我目前在我的环境中遇到了一些问题(eclipse崩溃等),所以我无法发布完整的日志。
以下是代码和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>();
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.advanced);
initLayout();
initVariables();
}
受保护的void initLayout(){
m_vwJokeButton=(按钮)findViewById(R.id.addJokeButton);
m_vwJokeEditText=(EditText)findViewById(R.id.newJokeEditText);
m_vwJokeLayout=(线性布局)findViewById(R.id.jokeListLayout);
}
受保护的void initVariables(){
Resources=getResources();
author=resources.getString(R.string.author\u name);
m_nDarkColor=resources.getColor(R.color.dark);
m_nLightColor=resources.getColor(R.color.light);
m_arrJokeList=新的ArrayList();
}
布局
<?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>
如果在运行时找不到R.string.author\u name,我将抛出一个异常。你可以抓住这个例外
try {
author = resources.getString(R.string.author_name);
} catch (Resources.NotFoundException e) {
}
但问题应该是为什么你的R.string.author\u名字不见了?您是否能够成功获取任何字符串资源?可能是在调用
initVariables()
期间导致的一些异常?运行时是否有任何logcat消息?请在模拟器或设备上发布logcat和您的advanced.xml。如果我试图在我的计算机上加载模拟器,它就会死掉。