Loading Spinner with ArrayList is giving exception of Unable to start activity ComponentInfo and app crashes

StackOverflow https://stackoverflow.com/questions/23318177

سؤال

I have brought some data from webservice in the form of json and converted into ArrayList. But when I load it into Spinner with Adapter, it is giving Unable to start activity ComponentInfo... and other execptions. I am posting my code below.

 <Spinner
    android:id="@+id/spin_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext" />

LoadViolationTypes() method:

public void LoadViolationTypes(){

    AsyncSpinnerLoader asl = new AsyncSpinnerLoader();
    asl.execute();
    String res="";
    try {
        res = asl.get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     ArrayList<String> itemsList = new ArrayList<String>();
        try {
            JSONArray array = new JSONArray(res);

            for (int i=0; i < array.length(); i++) {
                String s = (String) array.get(i);
                itemsList.add(s);
            }

            Spinner spinner = (Spinner) findViewById(R.id.vehicle_type);
            ArrayAdapter<String> adapter =
                    new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, itemsList);
            spinner.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }   
}
هل كانت مفيدة؟

المحلول

Your spinner id is spin_type, not vehicle_type. So likely it's null. Use

Spinner spinner = (Spinner) findViewById(R.id.spin_type);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top