Spinner无法在Android中运行

时间:2016-04-05 14:48:02

标签: android android-spinner

我的应用程序中有两个微调器。当我从一个微调器中选择一个值时,根据选择,值将被加载到第二个微调器。问题是当我选择第二个微调器时,应用程序不幸停止。提前谢谢。

public class CreateAppointment extends AppCompatActivity {

String[] lect_name = new String[100];
String[] spinner_subjects_load;
Spinner spSubject,spLecturer;
String chosedSubject;
String chosedLectName;
String chosedDateString;
RequestQueue requestQueue;
String url = "http://quick-appointment.b2creations.net/getLecturerNames.php";
CustomRequest customRequest;
String dayOfWeekString;
Calendar currentDate,chosedDate;
User user;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_appointment);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent intent = getIntent();
    user = (User) intent.getExtras().getSerializable("user");

    currentDate = Calendar.getInstance();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.format(currentDate.getTime());   //get current date


    spinner_subjects_load = new String[]{"SPDC", "HCI", "MAD", "SEIII", "SEP", "DAA", "ITP"};

    spSubject = (Spinner) findViewById(R.id.spSubject);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_subjects_load);
    spSubject.setAdapter(adapter1);

    requestQueue = Volley.newRequestQueue(this);

    spSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            chosedSubject = spSubject.getSelectedItem().toString();

            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("subj_name", chosedSubject);

            customRequest = new CustomRequest(Request.Method.POST, url, hashMap, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    int count = 0;
                    try {

                        JSONArray jsonArray = response.getJSONArray("Lecturers");
                        while (count < jsonArray.length()) {
                            JSONObject jo = jsonArray.getJSONObject(count);
                            lect_name[count] = jo.getString("name");
                            count++;
                        }

                        spLecturer = (Spinner) findViewById(R.id.spLecturer);

                        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(CreateAppointment.this, android.R.layout.simple_spinner_item, lect_name);
                        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spLecturer.setAdapter(adapter2);

                        chosedLectName = spLecturer.getSelectedItem().toString();
                        Toast.makeText(CreateAppointment.this, chosedLectName, Toast.LENGTH_SHORT).show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

            requestQueue.add(customRequest);

        }


        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

1 个答案:

答案 0 :(得分:0)

看起来你初始化了第二个微调器spLecturer,然后在你调用spLecturer.getSelectedItem().toString()之后立即调用,我相信此时会返回null。

也许您打算先向其添加OnItemSelectedListener