Skip to content Skip to sidebar Skip to footer

Android Spinner Crashing When Clicked On

I have a spinner on my page, which is populated using an arrayAdapter. When i launch my app everything is ok, but the moment i click on the spinner, the app crashes and logcat give

Solution 1:

if(arrayspinner[0]==null){
        arrayspinner = newString[1];
        arrayspinner[0] = "No Profiles, Please Create One";
    }

change your code and try

EDIT:

instead of Array try using ArrayList

List<String> arrayList=Arrays.asList(your stringarray);

or else try to initialize your array by getting the size of your response.

EDIT 2:

List<String> a= Arrays.asList(newString[]{});
        if(a.size()==0){
            a=new ArrayList<String>();
        a.add("xxx");
        a.add("bbbb");
        }
        final Spinner s=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, a);
        array.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(array);
        s.post(new Runnable() {

            @Override
            publicvoid run() {
                // TODO Auto-generated method stub
                s.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    publicvoid onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    publicvoid onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });
            }
        });

tried a sample same as your code and it works.

EDIT 3:

publicString[] getProfiles() throws SQLException{
        String[] columns = newString[]{KEY_USER};
        Cursor c = ourDatabase.query(TABLE_USERS, columns, null, null, null, null, KEY_ROWID);
        String[] result;
        if(c != null && c.getCount()!=0){
        result = newString[c.getCount()];

        int iName = c.getColumnIndex(KEY_USER);
        int count = 0;
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
            result[count] = c.getString(iName);
            count++;
        }
        }else{
        result = newString[]{"No Profiles, Please Create One"};
        }
        return result;
    }

Solution 2:

Change if condition inside onItemSelected as :

if(profiles.getItemAtPosition(pos).
                       toString().equals("No Profiles, Please Create One")){
   // your code here....
 }

because currently you are using ListView.getItemAtPosition for getting selected item from Spinner

Post a Comment for "Android Spinner Crashing When Clicked On"