0

基本上我的程序在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;
    }
}
4

2 回答 2

1

编辑 1

您必须始终以这种方式使用包名

String str  = getPackageName();

您是否缺少此导入

com.packtpub.kitchendroid.R;

这放置在 YourProjectFolder > gen > {yourPackageName}


这一定是抛出 IndexOutOfBound 异常

questions[index]

在这

  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");
    }
于 2013-09-12T00:13:35.607 回答
1

包名称不同。在 getIdentifier 中您提到了 "com.packpub.kitchenroid" 但实际的包名称是 "com.packtpub.kitchenroid" 。不是 packpub 中缺少的 't' :)

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.packtpub.kitchendroid");
}
于 2013-09-12T00:28:10.780 回答