【问题标题】:Getting Null values while Passing datas through Intent to another activity通过 Intent 将数据传递给另一个活动时获取 Null 值
【发布时间】:2012-02-24 05:20:29
【问题描述】:

在将数据通过 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);

在 2 Activity 中检索这里:

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

【问题讨论】:

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


    【解决方案1】:

    做这样的事情:

    活动一:

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

    活动二:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我用过,int id = getIntent().getExtras().getInt("id");即使我得到空值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多