Unable to add window token null is not for an application
AlertDialog.Builder localBuilder = new AlertDialog.Builder(Service_Activity.this);
// 这里使用getApplicationContext() 报错 必须使用当前activity。
———————————————————————————————————————————————————–
textView1.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
AlertDialog.Builder localBuilder= new AlertDialog.Builder(Service_Activity.this);
localBuilder.setTitle(“编辑地址”).setIcon(R.mipmap.bianji);
//final LinearLayout layout_alert= (LinearLayout) getLayoutInflater().inflate(R.layout.layout_alertservicejiekou, null);
localBuilder.setView(layout_alert);
localBuilder.setPositiveButton(“确定”, newDialogInterface.OnClickListener()
{public void onClick(DialogInterface paramAnonymousDialogInterface, intparamAnonymousInt)
{
// 这里使用(LinearLayout) getLayoutInflater().inflate(R.layout.layout_alertservicejiekou, null).findViewByid(R.id.edit1)
// .getText().toString()是得不到数据的
EditText edit1=(EditText) layout_alert.findViewById(R.id.edit1);
System.out.println(“*out”+edit1.getText().toString());
textView1.setText(edit1.getText().toString().trim());
}
}).setNegativeButton(“取消”, newDialogInterface.OnClickListener()
{public void onClick(DialogInterface paramAnonymousDialogInterface, intparamAnonymousInt)
{
}
}).create().show();
——————————————————————————————————————
1.这里的type初始值为null,则在第一次点击是toast的值是*null,然后选择tsg,那么下一次toast值才是tsg,总是得不到本次选择的值.?
在没有完成alertdialog选择时,程序就往下跑去了…… 吧操作写在alert里面吧,感觉又不大好……
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder localBuilder = new AlertDialog.Builder(MainActivity.this);
final String[] arrayOfString = { “ATPCU”, “C2CU”, “SDP”, “TSG”, “COMC”,”BTM” };
localBuilder.setTitle(“选择模块”).setIcon(R.mipmap.ic_launcher).setItems(arrayOfString, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
// Toast.makeText(MainActivity.this, “你选择了: ” + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();
type=arrayOfString[paramAnonymousInt];
}
}).create().show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),”*”+type,Toast.LENGTH_SHORT).show();
}
});
AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setTitle(“简单对话框”);
localBuilder.setIcon();
localBuilder.setMessage(“1+1=2 ?”);
localBuilder.setPositiveButton(“是”, newDialogInterface.OnClickListener()
{public void onClick(DialogInterface paramAnonymousDialogInterface, intparamAnonymousInt)
{
Toast.makeText(AlertActivity.this, “回答正确”, 0).show();
}
});
localBuilder.setNegativeButton(“不是”, newDialogInterface.OnClickListener()
{public void onClick(DialogInterface paramAnonymousDialogInterface, intparamAnonymousInt)
{
Toast.makeText(AlertActivity.this, “回答错误 重新选中”, 0).show();
}
});
localBuilder.create().show();
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211350.html原文链接:https://javaforall.net
