2012-02-24 64 views
0

将数据通过Intent从1个活动传递到另一个活动时,我得到空值。将数据通过意图传递给另一个活动时获取空值

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

Retriveing在这里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); 

回答

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"); 
    } 
0

为什么要创建束只使用意图。

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

然后在检索 – 2012-02-24 06:47:00

+0

我已经使用,INT ID = getIntent()。 。getExtras()getInt( “ID”);尽管得到nullvalue – 2012-02-24 06:54:27

+0

您的检索方法是好的,它应该工作。 – Altaf 2012-02-24 07:40:40

相关问题