对话框中的Android微调器出现此错误Android.view.WindowManager$BadTokenException:无法添加窗口
我需要在我的应用程序中显示一个对话框。在此对话框中有一个微调器。因此,我使用以下代码显示对话框并填充微调器:对话框中的Android微调器出现此错误Android.view.WindowManager$BadTokenException:无法添加窗口,android,dialog,spinner,Android,Dialog,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
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:无法添加窗口
我也发现了这个线索,但我不明白解决办法
解决
而不是
LayoutInflater LayoutInflater=LayoutInflatergetApplicationContext.getSystemServiceContext.LAYOUT\u充气机\u服务
使用
LayoutInflater LayoutInflater=getLayoutInflater 在spinner.setOnItemSelectedListener中,不要调用getApplicationContext,而是编写以下内容:
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();
}
尝试在不同的上下文中添加窗口 替换行ArrayAdapter=new ArrayAdapterview.getContext,android.R.layout.simple\u spinner\u项,matrie 与下面的一个:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(setup4.this /**Your activity_name.this*/,android.R.layout.simple_spinner_item, materie);
让我知道它是否有效…问题不在于吐司,因为它在打开的对话框中显示正确。然而,我尝试了你的解决方案,但错误仍然是一样的。谢谢你的建议:不要工作:我尝试了很多上下文,比如setup4.this,view.getContext,但它总是崩溃。我还尝试删除微调器的事件OnItemSelectedListener和onNothingSelected,但不管它如何崩溃:还有其他建议吗?