:
您好我在AlertDialog.Builder
中使用微调器来显示要选择的选项列表。但spinner
仅在未点击时显示String array
的第一项。如果单击,则显示强制关闭。我的代码如下。
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
我的输出:
如果我点击此微调器,强制关闭即将到来。
任何帮助都非常感谢并提前致谢
答案 0 :(得分:2)
尝试使用ClassName.this
而不是在声明LayoutInflater时使用getApplicationContext()
。
答案 1 :(得分:1)
我认为你在这里使用了错误的上下文尝试使用正确的上下文。
LayoutInflater factory = LayoutInflater.from(getApplicationContext());
而不是使用getApplicationContext()
尝试使用Activity_name.this or getParent();
答案 2 :(得分:0)
之前的builder.setView(v);两次拨打电话以删除不需要的电话。
错误必须在new MyOnItemSelectedListener()
或xml中设置条目。
同时共享两者和错误日志的代码。