gpt4 book ai didi

java - Android 程序在调用 getStringArray 后崩溃

转载 作者:行者123 更新时间:2023-12-02 06:44:57 25 4
gpt4 key购买 nike

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

最佳答案

包名称不同。在 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");
}

关于java - Android 程序在调用 getStringArray 后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com