我试图将我的一项活动中的字符串发送到该组的主要活动,但我遇到了一个小问题。我试图在我的代码的 onActivityResult 部分中创建一个对象(这似乎有效)。但是当试图从这个类中访问除 getName() 之外的方法时,它会吐出一个资源未找到异常。我确信解决方案很简单,但在仔细研究后它仍然让我难以理解。这是代码....
主要活动
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case 0:
//for add activity
if(resultCode == RESULT_OK)
{
namePasser = data.getExtras().getString("tallyName").toString();
itemHm.put("item1", new Items(namePasser,1));
itemAmt++;
currItem = itemHm.get("item1");
nameView1.setText(currItem.getName());
countView1.setText(currItem.getGroup());
count1b.setVisibility(View.VISIBLE);
reduce1b.setVisibility(View.VISIBLE);
countView1.setVisibility(View.VISIBLE);
resetb.setVisibility(View.VISIBLE);
}
}
}
添加活动
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class addActivity extends Activity
{
EditText name;
String itemName;
Button next;
Intent data;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.addactivityxml);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
name = (EditText)findViewById(R.id.newitemtxt);
itemName = name.getText().toString();
data = new Intent();
data.putExtra("tallyName", itemName);
setResult(RESULT_OK, data);
finish();
}
});
}
}
物品类
public class Items
{
int id = 0;
int group = 0;
String name = "";
int count = 0;
public Items(String name, int count)
{
this.count = count;
this.name = name;
}
public Items(String name, int count, int group)
{
this.group = group;
this.name = name;
this.count = count;
}
public Items(int id, String name, int group)
{
this.id = id;
this.group = group;
this.name = name;
}
public String getName()
{
return name;
}
public int getCount()
{
return count;
}
public int addCount()
{
count++;
return count;
}
public int reduceCount()
{
if(count > 0)
{
count--;
return count;
}
return count;
}
public int resetCount()
{
count = 0;
return count;
}
public void setGroup(int group)
{
this.group = group;
}
public int getGroup()
{
return group;
}
}
当我运行 nameView1.setText(currItem.getName()); 时,数据本身正在传递给 onActivity Result;没有问题,
但是当试图运行 countView1.setText(currItem.getGroup()); 那就是问题发生的时候
哦!这是错误日志
04-19 11:09:56.219: W/dalvikvm(1970): threadid=1: 线程退出未捕获异常 (group=0x409c01f8) 04-19 11:09:56.258: E/AndroidRuntime(1970): 致命异常: main 04-19 11:09:56.258: E/AndroidRuntime(1970): java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }}到活动 {com.DanielFurnell.iTallyLite/com.DanielFurnell.iTallyLite.ITallyLiteActivity}:android.content.res.Resources$NotFoundException:字符串资源 ID #0x0 04-19 11:09:56.258:E/AndroidRuntime(1970):在android.app.ActivityThread.deliverResults(ActivityThread.java:2980) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3023) 04-19 11: 09:56.258:E/AndroidRuntime(1970):在 android.app.ActivityThread。访问$1100(ActivityThread.java:123) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177) 04-19 11:09:56.258 : E/AndroidRuntime(1970): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.os.Looper.loop(Looper. java:137) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.app.ActivityThread.main(ActivityThread.java:4424) 04-19 11:09:56.258: E/AndroidRuntime(1970) : 在 java.lang.reflect.Method.invokeNative(Native Method) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 java.lang.reflect.Method.invoke(Method.java:511) 04- 19 11:09:56.258: E/AndroidRuntime(1970): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 04-19 11:09:56.258: E/AndroidRuntime(1970) :在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 dalvik.system.NativeStart.main(Native Method) 04-19 11:09:56.258:E/AndroidRuntime(1970):原因:android.content.res.Resources$NotFoundException:字符串资源 ID #0x0 04-19 11:09:56.258:E/AndroidRuntime(1970):在 android。 content.res.Resources.getText(Resources.java:247) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.widget.TextView.setText(TextView.java:3473) 04-19 11: 09:56.258: E/AndroidRuntime(1970): 在 com.DanielFurnell.iTallyLite.ITallyLiteActivity.onActivityResult(ITallyLiteActivity.java:73) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.app.Activity .dispatchActivityResult(Activity.java:4649) 04-19 11:09:56.258: E/AndroidRuntime(1970): 在 android.app.ActivityThread。deliverResults(ActivityThread.java:2976) 04-19 11:09:56.258: E/AndroidRuntime(1970): ... 11 更多
我也很确定该项目的文本框没有任何问题,因为我可以在 ActivityResult 之外为其分配其他值提前感谢任何可以帮助我解决此问题的人 =)