将数据传递给onActivityResult的问题

时间:2012-04-19 02:06:39

标签: java android object android-activity

我试图将我的一个活动中的字符串发送到该组的主要活动,但是我遇到了一个小问题。我试图在我的代码的onActivityResult部分创建一个对象(这似乎工作)。但是当尝试从getName()以外的类中访问方法时,它会发出资源未找到的异常。我确定解决方案很简单,但在对它进行了一些调查之后,它仍然无法避免。继承人代码......

MainActivity

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);

           }
       }
   }

addActivity

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;
    }
}

当我运行时,数据本身正在传递给onActivity Result nameView1.setText(currItem.getName()); 没问题,

但是在试图跑的时候 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):致命异常:主要   04-19 11:09:56.258:E / AndroidRuntime(1970):java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 0,result = -1,data = Intent {(has extras)}} to activity {com.DanielFurnell.iTallyLite / com.DanielFurnell.iTallyLite.ITallyLiteActivity}:android.content.res.Resources $ NotFoundException:String resource 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.access $ 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):at java.lang.reflect.Method.invokeNative(Native Method)   04-19 11:09:56.258:E / AndroidRuntime(1970):at java.lang.reflect.Method.invoke(Method.java:511)   04-19 11:09:56.258:E / AndroidRuntime(1970):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)   04-19 11:09:56.258:E / AndroidRuntime(1970):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)   04-19 11:09:56.258:E / AndroidRuntime(1970):at 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):at 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 more

我也很确定项目的文本框没有任何问题,因为我可以在ActivityResult之外为其分配其他值 在此先感谢任何可以帮助我解决这个问题的人=)

2 个答案:

答案 0 :(得分:2)

countView1.setText(currItem.getGroup());

可能是这行有问题,getGroup返回int类型,它认为是字符串id,所以你需要检查返回值是否是有效的字符串id < / strong>,如果您只想设置getGroup返回的内容,请尝试使用:

countView1.setText(currItem.getGroup()+"");

答案 1 :(得分:0)

name = (EditText)findViewById(R.id.newitemtxt);

从点击事件中删除此行,然后在下面添加按钮= ...

并尝试..