我有一个应用程序,其中许多活动打开另一个,其中一些东西设置在打开它的按钮(图像,标签等)后。在此活动关闭后,我想返回一个字符串vith值“1 “将转换为int并用于设置分数。
在返回的活动中我有这个:
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)
你能帮帮我吗?
答案 0 :(得分:2)
txtScore.setText(a);
a是Integer
。所以Android正在寻找带有该ID的R里面的String。
随
改变txtScore.setText(String.valueOf(a));
您使用的是setText(int),而不是setText(Charsequence)
答案 1 :(得分:2)
您应该使用以下
txtScore.setText(String.valueOf(a));
public final void setText (int resid)
http://developer.android.com/reference/android/widget/TextView.html
因此它会查找一个int值的资源ID。这不存在。因此,您将获得资源未找到异常
答案 2 :(得分:0)
请尝试txtScore.setText(“”+ a); 您不能直接将int设置为TextView