我在做Google Android App课程,我遇到了一个奇怪的错误。如果我拨打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>
答案 0 :(得分:0)
如果在运行时找不到R.string.author_name,我将抛出异常。你可以捕获异常
try {
author = resources.getString(R.string.author_name);
} catch (Resources.NotFoundException e) {
}
但问题应该是为什么你的R.string.author_name丢失了?你能成功获得任何字符串资源吗?