1

大家好,
这次我遇到了 Intent & Extras 的问题。

这是我的第一个活动

Intent i1 = new Intent(RoomSetting.this, App.class);
i1.putExtra("subposition1", "right");
final int result=1;
startActivityForResult(i1, result);

在第二个活动中,我尝试获得“subposition1”值

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d(); 
    Bundle extras = getIntent().getExtras();
    String SubPos = extras.getString("subposition1");
}

我的问题是,如何在另一种方法上使用 SubPos?

对不起,noobish 问题。提前致谢

4

5 回答 5

1
String SubPos;
// declare SubPos String as class variable and you can access anywhere in class<<<
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d(); 
    Bundle extras = getIntent().getExtras();
    SubPos = extras.getString("subposition1");
}

public void anotherMethod(){
//here you can access it...
}
于 2012-06-20T13:50:50.347 回答
1

之前声明SubPosonCreate为:

public String SubPos=null; // or you can also declare it as static as your need
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d(); 
    Bundle extras = getIntent().getExtras();
    SubPos = extras.getString("subposition1");
}


   public void testmethod()
   {
      String str=SubPos; //get SubPos value  
    }
于 2012-06-20T13:51:06.857 回答
1

有两种方法可以轻松做到:

第一种方式:为类使用全局变量:

   String SubPos;
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d(); 
    Bundle extras = getIntent().getExtras();
    SubPos = extras.getString("subposition1"); //note that I've deleted the 'String'
}

2n方式:将变量传递给方法:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d(); 
    Bundle extras = getIntent().getExtras();
    String SubPos = extras.getString("subposition1");
    otherMethod(SubPos);
}

public void otherMethod(String s) {
    //do stuff.
}

我认为也许你想在另一个类中使用这个变量,然后你可以添加这个函数(在类中将 String SubPos 作为全局):

   String SubPos;
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccVals = new Number3d(); 
    Bundle extras = getIntent().getExtras();
    SubPos = extras.getString("subposition1"); //note that I've deleted the 'String'
   }
    public String getSubPos() {
        return SubPos;
    }

这样,您可以在任何其他具有该实例的类中调用 instanceOfTheClass.getSubPos()。

于 2012-06-20T13:56:26.647 回答
1

代替

Bundle extras = getIntent().getExtras();
String SubPos = extras.getString("subposition1");

String SubPos = getIntent().getStringExtra("subposition1");
于 2012-06-20T13:56:36.990 回答
1

只需在您的“其他方法”上调用它即可。

String subposition = getIntent().getStringExtra("subposition1");
于 2012-06-20T14:14:49.777 回答