通过Intent将数据传递给另一个活动时获取Null值

时间:2012-02-24 05:20:30

标签: android android-layout android-intent android-emulator

我通过Intent将数据从1个活动传递到另一个活动时获得null值。

从1Activity传递:

                       int s = position;    
          String str=adapter.getId(position);   
                int Type=DeviceType;
                Bundle bunch=new Bundle();
                bunch.putString("id", str);
                bunch.putInt("DeviceType",Type);
                bunch.putInt("position", s);
                Intent It=new Intent();
                It.setClass(PYSActivity.this,GridImages.class);
                It.putExtras(bunch);
                startActivity(It);

在2活动中回复:

               super.onCreate(savedInstanceState);  
    setContentView(R.layout.main1);

               Bundle b = this.getIntent().getExtras();
               AppID=b.getString("id");
               DeviceType=b.getInt("DeviceType");
               Position=b.getInt("position");

               list=(GridView)findViewById(R.id.list);
    adapter1=new LazyAdapter1(this,mStrings,like,Rate,Id,img); 
    list.setAdapter(adapter1);   

2 个答案:

答案 0 :(得分:1)

做这样的事情:

活动1:

    int myInt = 5;
    String myString = "hi";

    ...
    Intent Intent = new Intent(...);
    intent.putExtra("string_key", myString);
    intent.putExtra("int_key", myInt);
    startActivity(intent);

活动2:

    int getInt;
    String getString;
    ...

    Bundle extras = getIntent().getExtras();

    // Read the extras data if it's available.
    if (extras != null)
    {
        getInt = extras.getInt("int_key");
        getString = extras.getString("string_key");
    }

答案 1 :(得分:0)

为什么要创建捆绑只是使用意图。

            it.putExtra("id", str);
            it.putExtra("DeviceType",Type);
            it.putExtra("position", s);