gpt4 book ai didi

java - 在 Android 应用程序中调用 string.xml 文件中的数组中的值

转载 作者:行者123 更新时间:2023-12-01 22:38:00 25 4
gpt4 key购买 nike

我构建了一个财富厨师应用程序,之前该应用程序的值已硬编码到数组中

FortuneActivity.java

package juangallardo.emofortunecookie;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;


public class FortuneActivity extends Activity {

private FortuneBox mFortuneBox = new FortuneBox();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fortune);

// Declare our View variables and assign them the Views from the layout file
final TextView fortuneLabel = (TextView) findViewById(R.id.fortuneTextView);
Button showFortuneButton = (Button) findViewById(R.id.showFortuneButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fortune = mFortuneBox.getFortune();
// Update the label with dynamic fortune
fortuneLabel.setText(fortune);
}
};
showFortuneButton.setOnClickListener(listener);

}
}

FortuneBox.java

package juangallardo.emofortunecookie;

import java.util.Random;

public class FortuneBox {

public String[] mFortunes = {

"What is the point?",
"Sometimes it is best to just sleep in.",
#98 other fortunes...
};

// Methods (abilities)
public String getFortune() {

String fortune = "";

// Randomly select a fortune
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFortunes.length);

fortune = mFortunes[randomNumber];

return fortune;
}
}


<小时/>

问题是现在我想添加西类牙语版本。所以我意识到我应该将该数组添加到 strings.xml 中。

我在 Android developer page 上查找了字符串资源。这让我想到将其添加到我的代码中

strings.xml

<string-array name="emo_fortunes">
<item>What is the point?</item>
<item>Sometimes it is best to just sleep in.</item>
</string-array>

但现在我不知道在哪里添加这部分,其中包含有关资源等的部分。

enter image description here

我按照 Treehouse 的有关字符串的教程进行操作,但我的应用程序不断崩溃。

基本上我所做的更改是将原始数组变成

FortuneBox.java

# above unchanged from previous code
public String[] mFortunes;
# below unchanged from previous code

FortuneActivity.java

# same imports as before
public class FortuneActivity extends Activity {

private FortuneBox mFortuneBox = new FortuneBox();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fortune);

Resources resources = getResources();
final String[] mFortuneBox = resources.getStringArray(R.array.emo_fortunes);

// Declare our View variables and assign them the Views from the layout file
final TextView fortuneLabel = (TextView) findViewById(R.id.fortuneTextView);
Button showFortuneButton = (Button) findViewById(R.id.showFortuneButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fortune = mFortuneBox.getFortune();
// Update the label with dynamic fortune
fortuneLabel.setText(fortune);
}
};
showFortuneButton.setOnClickListener(listener);

}
}

这些是我的错误,但不知道该从哪里开始,因为我是 Android 新手,而且自从大学以来我就没有接触过 Java。

日志

enter image description here

FortuneActivity.java

enter image description here

FortuneBox.java

enter image description here

最佳答案

Mikki 给出了正确的答案,但有点令人困惑。在上面的代码中,您对两个不同的变量使用相同的名称:mFortuneBox。这是你麻烦的根源:

private FortuneBox mFortuneBox = new FortuneBox();
...
final String[] mFortuneBox = resources.getStringArray(R.array.emo_fortunes);

将第二个名称更改为不同的名称,如下所示,错误就会消失:

final String[] fortunes = resources.getStringArray(R.array.emo_fortunes);

但是,您仍然没有在任何地方使用数组中的这些财富。实际上,您可以从 Activity 中删除 fortunes 并将其移动到 FortuneBox 类中。不过,这有点棘手,因为您需要知道上下文是什么才能获取其他类中的字符串数组资源。 上下文 是 Activity,因此您需要在创建 FortuneBox 对象时将其作为参数传递。

我建议进行一些轻微的重组。以下是应该适合您的两个文件:

FortuneActivity.java

public class FortuneActivity extends Activity {

private FortuneBox mFortuneBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fortune);

mFortuneBox = new FortuneBox(this);

// Declare our View variables and assign them the Views from the layout file
final TextView fortuneLabel = (TextView) findViewById(R.id.fortuneTextView);
Button showFortuneButton = (Button) findViewById(R.id.showFortuneButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fortune = mFortuneBox.getFortune();
// Update the label with dynamic fortune
fortuneLabel.setText(fortune);
}
};
showFortuneButton.setOnClickListener(listener);

}
}

FortuneBox.java

public class FortuneBox {

public String[] mFortunes;

public FortuneBox(Context context) {
Resources resources = context.getResources();
mFortunes = resources.getStringArray(R.array.emo_fortunes);
}

// Methods (abilities)
public String getFortune() {

String fortune = "";

// Randomly select a fortune
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFortunes.length);

fortune = mFortunes[randomNumber];

return fortune;
}
}

关于java - 在 Android 应用程序中调用 string.xml 文件中的数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26622237/

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