gpt4 book ai didi

java - 当我尝试通过 Intent 发送字符串时出错

转载 作者:行者123 更新时间:2023-12-01 18:46:51 25 4
gpt4 key购买 nike

我有一个应用程序,其中许多 Activity 打开另一个 Activity ,其中在打开它的按钮之后设置了一些内容(图像、标签等)。此 Activity 关闭后,我希望返回一个字符串 vith 值“1” "它将被转换为 int 并用于设置分数。

在返回的 Activity 中我有这个:

Intent i;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vie);
img = (ImageView) findViewById(R.id.img);
et = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btnCheck);
txt = (TextView) findViewById(R.id.txt);

win_sound = MediaPlayer.create(Vie.this, R.raw.win);
wrong_sound= MediaPlayer.create(Vie.this, R.raw.wrong);

i = getIntent();
Bitmap back = i.getParcelableExtra("back");
Drawable b = new BitmapDrawable(getResources(), back);
img.setImageDrawable(b);
String tag = i.getStringExtra("tag");
Object tag2 = (Object) tag;
img.setTag(tag2);
btn.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean mb = check(et, img);
if (mb) {
// Sunet toast thread
txt.setText(title(img));
win_sound.start();
i.putExtra("score", "1");
setResult(RESULT_OK, i);

Thread t = new Thread() {

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sleep(win_sound.getDuration());

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
finish();
}

}

};

t.start();

}

if (!mb) {
// sunet toast thread
wrong_sound.start();
Toast t = Toast.makeText(getApplicationContext(),"Wrong answer! Please check if you have spelled corectly the name of the team!",Toast.LENGTH_LONG);
t.show();
}
}

以及 OnActivityResult 方法:

int a = 0
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data.getExtras().containsKey("score")) {
a +=Integer.valueOf(data.getStringExtra("score"));
}

}

并且 textView 设置为显示:

txtScore.setText(a);

但这给了我一个错误,这是我的 LogCat:

06-29 15:28:25.312: E/AndroidRuntime(981): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
06-29 15:28:25.312: E/AndroidRuntime(981): at android.content.res.Resources.getText(Resources.java:230)
06-29 15:28:25.312: E/AndroidRuntime(981): at android.widget.TextView.setText(TextView.java:3769)
06-29 15:28:25.312: E/AndroidRuntime(981): at com.RandDdev.uclquiz.Teams.onCreate(Teams.java:61)

你能帮我吗?

最佳答案

txtScore.setText(a); 

a 是一个整数。因此 Android 正在 R 中寻找具有该 id 的字符串。

改变

txtScore.setText(String.valueOf(a));

您正在使用setText(int) ,而不是 setText(Charsequence)

关于java - 当我尝试通过 Intent 发送字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17381825/

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