基本上我的程序在android模拟器中崩溃并显示错误消息。不确定是什么问题。
日志猫:
E/AndroidRuntime( 1928): FATAL EXCEPTION: main
E/AndroidRuntime( 1928): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.packtpub.kitchendroid/com.packtpub.kitchendroid.QuestionActivity}: android.content.res.Resources$NotFoundException: String array resource ID #0x0
Java代码
package com.packtpub.kitchendroid;
import android.app.Activity;
import android.os.Bundle;
import android.content.res.Resources;
import android.widget.Button;
import android.widget.TextView;
import android.view.ViewGroup;
public class QuestionActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initQuestionScreen();
}
private Button[] buttons;
private static int getQuestionID(final Resources res, final int index) {
final String[] questions = res.getStringArray(R.array.questions);
return res.getIdentifier(questions[index],
"array",
"com.packpub.kitchenroid");
}
private int getQuestionIndex() {
return getIntent().getIntExtra("KitchenDroid.Question", 0);
}
private void initQuestionScreen() {
TextView question = (TextView)findViewById(R.id.question);
ViewGroup answers = (ViewGroup)findViewById(R.id.answers);
Resources resources = getResources();
int questionID = getQuestionID(resources, getQuestionIndex());
//Crashes after below line of code
String[] quesionData = resources.getStringArray(questionID);
// question.setText(quesionData[0]);
// int answerCount = quesionData.length - 1;
// buttons = new Button[answerCount];
// for(int i = 0; i < answerCount; i++) {
// String answer = quesionData[i + 1];
// Button button = new Button(this);
// button.setText(answer);
// answers.addView(button);
// buttons[i] = button;
// }
}
}
我的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">QuestionActivity</string>
<string-array name="questions">
<item>vegetarian</item>
<item>size</item>
</string-array>
<string-array name="vegetarian">
<item>Are you a Vegetarian?</item>
<item>Yes</item>
<item>No</item>
<item>I\'m a vegan</item>
</string-array>
<string-array name="size">
<item>How much do you feel like eating?</item>
<item>A large meal</item>
<item>Just a nice single serving of food</item>
<item>Some finger foods</item>
<item>Just a snack</item>
</string-array>
</resources>
我的 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait..."
/>
<LinearLayout
android:id="@+id/answers"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/yes"
android:text="Yes!"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/no"
android:text="No!"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/maybe"
android:text="Maybe?"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/skip"
android:layout_marginTop="12sp"
android:text="Skip Question"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/view"
android:layout_marginTop="12sp"
android:text="Feed Me!"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.packtpub.kitchendroid;
public final class R {
public static final class array {
public static final int questions=0x7f050000;
public static final int size=0x7f050002;
public static final int vegetarian=0x7f050001;
}
public static final class attr {
}
public static final class dimen {
public static final int half_width=0x7f030000;
}
public static final class id {
public static final int answers=0x7f060001;
public static final int maybe=0x7f060004;
public static final int no=0x7f060003;
public static final int question=0x7f060000;
public static final int skip=0x7f060005;
public static final int view=0x7f060006;
public static final int yes=0x7f060002;
}
public static final class layout {
public static final int main=0x7f020000;
}
public static final class string {
public static final int app_name=0x7f040000;
}
}