我正在尝试获取先前活动提供的参数。参数的数量不固定,我的方法应该能够读取所有参数(从 1 到 n)。参数的数量由参数 n_inputs 给出。我试过用下面的代码来做,编译器是正确的,但它有一些问题,我不知道在哪里......我认为它必须与数组 param[] 相关。请问你能帮帮我吗??
double params[];
int n_inputs;
@Override
protected void onCreate(Bundle savedInstanceState)
{
//I'm gonna get the parameters from the previous activity
Bundle b = getIntent().getExtras();
//here I get the number of inputs from the previous activity
n_inputs = b.getInt("ninputs");
// I create a new array with dimension "i_inputs"
params= new double[n_inputs];
if(n_inputs>0)
{
for (int i=0;i<n_inputs;i++)
{
params[i] = b.getDouble("param"+(i+1));
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//Now I try to show the parameters in a toast
Context context = getApplicationContext();
CharSequence text = "parameter 1 "+params[0] +" param 2 "+ params[1] ;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
非常感谢!!