0

i need to show a Dialog in my application. In this dialog there is a Spinner. So i use this code to show the dialog and fill the Spinner:

public class setup4 extends Activity {

public List<String> materie = new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setup4);

    Database d = new Database(this);
    SQLiteDatabase db = d.getWritableDatabase();
    Cursor cursor = db.rawQuery("select * from materie", null);
    if (cursor.moveToFirst()) {
        do materie.add(cursor.getString(1)); while (cursor.moveToNext());
    }
    db.close();
}



//On bottone setup 4
public void onSetup4bottone(View v)
{
    AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
    customDialog.setTitle("Aggiungi ora scolastica");
    LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.inflate(R.layout.aggiungi_ora,null);
    customDialog.setView(view);

    Spinner spinner = (Spinner) view.findViewById(R.id.aggiungi_ora_materia);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item, materie);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            String selected = (String) parentView.getItemAtPosition(position);
            Toast.makeText(
                    getApplicationContext(), 
                    "hai selezionato "+selected, 
                    Toast.LENGTH_LONG
                ).show();
        }

        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

    customDialog.show();
}
}

the spinner loads items correctly but when i click on it to change the value the application crash with this error: android.view.WindowManager$BadTokenException: Unable to add window I also find this thread Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window but i can't understand the solution

SOLVED Instead of LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

use

LayoutInflater layoutInflater = getLayoutInflater();

Community
  • 1
  • 1
barbo
  • 5
  • 4

2 Answers2

1

Try to add your window in a different context.

Replace your line ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item, materie);

with the following one:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(setup4.this /**Your activity_name.this*/,android.R.layout.simple_spinner_item, materie);

Let me know if it works...

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • Don't work :( I try many context like this, setup4.this, view.getContext() but it always crash. I also try to remove the spinner's event (OnItemSelectedListener and onNothingSelected) but however it crash :( Any other suggestions? – barbo Aug 06 '12 at 06:09
0

In your spinner.setOnItemSelectedListener(), don't call getApplicationContext() but instead write this:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String selected = (String) parentView.getItemAtPosition(position);
        Toast.makeText(
                setup4.this,     //<===THIS LINE IS CHANGED BY ME
                "hai selezionato "+selected, 
                Toast.LENGTH_LONG
            ).show();
    }
tolgap
  • 9,629
  • 10
  • 51
  • 65
  • The problem isn't the toast because it's shown correctly on the dialog opening. However i tried your solution but the error remain the same. Thanks for the suggestion :) – barbo Aug 06 '12 at 06:06