我创建了以下活动,该活动使用跨多个活动的意图传递Edittext值的数据。我希望能够根据微调器的用户选择来填充EditText值。我在下面尝试了以下代码,但遇到了OnItemSelectedListener问题。有什么建议吗?
public class Fragmentation_Rock_Properties extends AppCompatActivity {
Spinner RockTypeSpinner;
EditText RockDensityEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentation_rock_properties);
//set spinner and edittext
RockTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if (RockTypeSpinner.getSelectedItem().toString().equals("Sandstone")) {
RockDensityEditText.setText("1");
} else if (RockTypeSpinner.getSelectedItem().toString().equals("Mudstone")) {
RockDensityEditText.setText("2");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//Set intent to send data
final Intent i = new Intent(this, Fragmentation_Rock_Jointing.class);
Button button = findViewById(R.id.IntactRockNextbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Declare EditText
EditText rockdensityedittext = findViewById(R.id.RockDensityEditText);
EditText youngsmodulusedittext = findViewById(R.id.YoungsModulusEditText);
EditText ucsedittext = findViewById(R.id.UCSEditText);
//Set intent to pass edittexts
i.putExtra("rockdensity", rockdensityedittext.getText().toString());
i.putExtra("youngsmodulus", youngsmodulusedittext.getText().toString());
i.putExtra("ucs", ucsedittext.getText().toString());
startActivity(i);
}
}
);
}
}
答案 0 :(得分:0)
RockTypeSpinner
和RockDensityEditText
是null
。要使用变量,我们需要对其进行初始化。看我的代码。它可以帮助您。
public class Fragmentation_Rock_Properties extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentation_rock_properties);
final EditText rockdensityedittext = findViewById(R.id.RockDensityEditText);
final EditText youngsmodulusedittext = findViewById(R.id.YoungsModulusEditText);
final EditText ucsedittext = findViewById(R.id.UCSEditText);
// Replace ID_OF_YOUR_SPINNER by real value
final Spinner rockTypeSpiner = findViewById(R.id.ID_OF_YOUR_SPINNER);
rockTypeSpiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String selectedItem = rockTypeSpiner.getSelectedItem().toString();
if (selectedItem.equals("Sandstone")) {
rockdensityedittext.setText("1");
} else if (selectedItem.equals("Mudstone")) {
rockdensityedittext.setText("2");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Set intent to send data
final Intent i = new Intent(this, Fragmentation_Rock_Jointing.class);
Button button = findViewById(R.id.IntactRockNextbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set intent to pass edittexts
i.putExtra("rockdensity", rockdensityedittext.getText().toString());
i.putExtra("youngsmodulus", youngsmodulusedittext.getText().toString());
i.putExtra("ucs", ucsedittext.getText().toString());
startActivity(i);
}
}
);
}
}