我的活动中有三个微调器。因此,我必须根据三个微调器的输入来打开特定的活动。如果我在第二个微调器中的第一个微调器中输入“ 1”,在第二个微调器中输入“ cse”,在第三个微调器中输入“ b”,则必须打开受关注的活动,以此类推。
我尝试使用if语句,此后每次应用程序都崩溃。
请当心代码中的注释,这些是我尝试过但没有得到任何结果的代码。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
/*TextView selection1;
TextView selection2;
TextView selection3;*/
Spinner spin1;
Spinner spin2;
Spinner spin3;
String[] years = {"1","2","3","4"};
String[] branches = {"CSE","ECE","EEE","CIVIL","IT","MECH"};
String[] sections = {"A","B","C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
spin1.setOnItemSelectedListener(this);
/*List<String> branches = new ArrayList<String>();
branches.add("CSE");
branches.add("ECE");
branches.add("IT");
branches.add("MECH");
branches.add("CIVIL");
branches.add("EEE");*/
ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching dataAdapter to spinner
spin1.setAdapter(dataAdapter);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
spin2.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branches);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(dataAdapter2);
Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
spin3.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sections);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(dataAdapter3);
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
/*Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
Spinner s3 = (Spinner)findViewById(R.id.spinner3);
String sp1 = s1.getPrompt().toString();
String sp2 = s2.getPrompt().toString();
String sp3 = s3.getPrompt().toString();
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){*/
Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
Intent it = new Intent(this,M2.class);
startActivity(it);
//}
/*else{
Toast.makeText(this, "Invalid input", Toast.LENGTH_LONG).show();
}*/
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//on selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
/*if (spin1.getSelectedItem().toString().equals("3")) {
if (spin2.getSelectedItem().toString().equals("CSE")) {
if (spin3.getSelectedItem().toString().equals("B")) {*/
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
/*Intent it = new Intent(this, M2.class);
startActivity(it);*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*Button btn =(Button)findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
displaycheck();
}
}
);
}
public void displaycheck(){
Spinner s1 =(Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Spinner s3 = (Spinner) findViewById(R.id.spinner3);
String ye = y.getText().toString();
String br = b.getText().toString();
String se = s.getText().toString();
if (ye.equals("3")&&br.equals("cse")&&se.equals("b")) {
Toast.makeText(this, "ra ra ", Toast.LENGTH_LONG).show();
Intent it = new Intent(this, M2.class);
startActivity(it);
}
else{
Toast.makeText(this, "po po", Toast.LENGTH_LONG).show();
}
}*/
}
最佳答案
您没有为全局变量-活动范围-spin1, spin2, spin3
赋值。
相反,您正在为onCreate
或方法特定范围赋予价值;当您从中获取价值时,它永远不会被初始化。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
Spinner spin1;
Spinner spin2;
Spinner spin3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get spinner in variable
spin1 = (Spinner) findViewById(R.id.spinner1);
...
// Your adapter, ItemListener, DropDownViewResource
spin2 = (Spinner) findViewById(R.id.spinner2);
... // Your adapter, ItemListener, DropDownViewResource
spin3 = (Spinner) findViewById(R.id.spinner3);
... // Your adapter, ItemListener, DropDownViewResource
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
String sp1 = spin1.getSelectedItem().toString();
String sp2 = spin2.getSelectedItem().toString();
String sp3 = spin3.getSelectedItem().toString();
// Your combinations check
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){
// based on combination - redirect user
}
}
}