我收到此错误并且无法捕获异常。我真的很困惑为什么我不能 catch , Before Works.
这里我有 2 个 EditText 我想检查它是否为空做一些工作。我使用其他方法检查是否为空但不起作用并退出应用程序。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ad=new AlertDialog.Builder(getApplicationContext()).create();
ad.setTitle("خطا");
sp=sp=getSharedPreferences("sp", Activity.MODE_PRIVATE);
bsobh=(Button) findViewById(R.id.Bsobh);
bzohr=(Button) findViewById(R.id.Bzohr);
basr=(Button) findViewById(R.id.basr);
bmaqrib=(Button) findViewById(R.id.bmaqrib);
besha=(Button) findViewById(R.id.besha);
brooze=(Button) findViewById(R.id.brooze);
ok=(Button) findViewById(R.id.ok);
etnamaz=(EditText) findViewById(R.id.ettedaderoozenamaz);
etrooze=(EditText) findViewById(R.id.ettedadroozerooze);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
int temp=Integer.parseInt(etnamaz.getText().toString());
sobh=temp;
zohr=temp;
asr=temp;
maqrib=temp;
esha=temp;
}
catch(Exception e){
ad.setMessage("لطفا تعداد روزهایی که نمازتان قضا شده است را وارد کتید");
ad.show();
}
try{
rooze=Integer.parseInt(etrooze.getText().toString());
}
catch(Exception e){
ad.setMessage("لطفا تعداد روزهایی که روزه تان قضا شده است را وارد کتید");
ad.show();
}
inserted=true;
update();
}
});
}
最佳答案
works when edittext is not empty
问题:
int temp=Integer.parseInt(etnamaz.getText().toString());
当字符串为空时,您无法将字符串解析为 int
,您可能会得到 numberMismatchException
。
解决方法:
首先检查 etnamaz
或 etrooze
是否有一些值,然后再将其解析为 int。
示例:
if(etnamaz.getText().length() != 0)
//if it has some values
else
//if it doesnt have value
编辑:
您需要使用 this
ad=new AlertDialog.Builder(this).create();
关于安卓.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an application-(in Edit Text),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846715/