我需要在我的应用程序中显示一个对话框。在这个对话框中有一个微调。所以我用这个代码显示对话框,并填补了微调:正确
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();
}
}
微调加载项目,但是当我点击它来改变值与此错误的应用程序崩溃:android.view.WindowManager $ BadTokenException :无法添加窗口 我也发现这个线程Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window,但我不明白的解决方案
解决 而不是 LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ü SE
LayoutInflater layoutInflater = getLayoutInflater();
的问题是不是面包,因为它正确显示的对话框上开幕。不过,我试过你的解决方案,但错误保持不变。感谢您的建议:) – barbo 2012-08-06 06:06:09