0

我对Android真的很陌生。运行时出现此错误(应用程序崩溃)。我正在尝试通过调用 getResources() 来访问 main.java 中的字符串数组。但是会抛出 Resource Not Found Exception。请查看下面给出的我的代码:

 import android.os.Bundle;
    import android.content.res.Resources;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.ViewGroup;
    import android.widget.*;
   //import com.packtpub.kitchendroid.R;

    public class QuestionActivity extends Activity {

    private Button[] buttons;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        initQuestionScreen();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.question, menu);
        return true;
    }

    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.packtub.kitchendroid");
    }

    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 resources1 = getResources();
        int questionID = getQuestionId(resources1, getQuestionIndex());
        String[] questionData = resources1.getStringArray(questionID);
        question.setText(questionData[0]);
        int answerCount = questionData.length -1;
        buttons = new Button[answerCount];
        for(int i=0;i<answerCount;i++)
        {
            String answer = questionData[i+1];
            Button button = new Button(this);
            button.setText(answer);
            answers.addView(button);
            buttons[i]=button;

          }


            }
    }

我的xml如下:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string name="app_name">Kitchen Droid</string>
        <string name="action_settings">Settings</string>
        <string name="question">Please wait</string>
        <string name="skip">Skip Question</string>
        <string name="feedme">Feed Me!..</string>
        <string-array name="questions">
            <item>vegetarian</item>
            <item>size</item>
        </string-array>
        <string-array name="vegetarian">
             <item>Are you a vegeterian?</item>
             <item>Yes</item>
             <item>No</item>
             <item>I\'m a vegetarian</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>

请帮我解决这个问题。如果问题不合适,我深表歉意,因为我对 Android 完全陌生

4

1 回答 1

3

你的包名有错别字吗?似乎

res.getIdentifier(questions[index],"array","com.packtub.kitchendroid");

应该

res.getIdentifier(questions[index],"array","com.packtpub.kitchendroid");

或者,您可以使用:

res.getIdentifier(questions[index], "array", QuestionActivity.this.getPackageName());
于 2013-09-22T15:57:46.553 回答