更新:
嗨,我在AlertDialog.Builder中使用微调器以显示要选择的选项列表.但是微调器在未单击时仅显示String数组的第一项.如果单击,将显示强制关闭.我的代码如下.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater li=LayoutInflater.from(this);
View v=li.inflate(R.layout.searchme, null);
builder.setIcon(android.R.drawable.ic_input_get);
builder.setView(v);
builder.setTitle("Search");
LayoutInflater factory = LayoutInflater.from(getApplicationContext());
final View textEntryView = factory.inflate(R.layout.searchme, null);
builder.setView(textEntryView);
Spinner spin=(Spinner)textEntryView.findViewById(R.id.searchspinner);
Utilities.ManageDeptSpinner(this, spin);
for(int i=0;i<spin.getCount();i++)
{
long id=spin.getItemIdAtPosition(i);
spin.setSelection(i, true);
break;
}
spin.setOnItemSelectedListener(new MyOnItemSelectedListener());
builder.setPositiveButton("Go",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
try
{
titletext = (EditText) textEntryView.findViewById(R.id.titleText1);
persontext = (EditText) textEntryView.findViewById(R.id.personText2);
prioritytext = (EditText) textEntryView.findViewById(R.id.priorityText3);
title_text = titletext.getText().toString();
person_text = persontext.getText().toString();
priority_text = prioritytext.getText().toString();
String condition = "titlee='"+title_text+"' or pname ='"+person_text+"' or prior='"+priority_text+"'";
refresh_data(" ASC","prior",condition);
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
// Do nothing
}
});
AlertDialog alert = builder.create();
alert.show();
MyOnItemSelectedListener.java:
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
@SuppressWarnings("unused")
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
try
{
switch(parent.getId())
{
case R.id.searchspinner:
Toast.makeText(getApplicationContext(),"\n Selected : "+parent.getItemAtPosition(pos).toString()+"\n",Toast.LENGTH_LONG).show();
break;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing.
}
}
我得到如下异常:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我的输出:
如果单击此微调器,则将强制关闭.
非常感谢任何帮助,并在此先感谢
解决方法:
在声明LayoutInflator时,请尝试使用ClassName.this而不是使用getApplicationContext().
标签:alertdialog,java,android 来源: https://codeday.me/bug/20191202/2085805.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。