安卓.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an application-(in Edit Text)

标签 android exception android-edittext

我收到此错误并且无法捕获异常。我真的很困惑为什么我不能 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

解决方法:

首先检查 etnamazetrooze 是否有一些值,然后再将其解析为 int。

示例:

if(etnamaz.getText().length() != 0)
//if it has some values
else
//if it doesnt have value

编辑:

您需要使用 this

获取 Activity 上下文,而不是获取整个应用程序上下文
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/

相关文章:

java - 子/项目添加监听器gridview android?

java - android fragment 可以在api 3.0上使用,但不能在2.2上使用

android - 我如何调用 startActivity() 以使用来自不同类的 ACTION_SEND

c# - 获取对其构造函数抛出异常的实例的引用

php - fatal error : uncaught exception without try/catch block

android - 我们可以在同一个EditText中写入彩色文本吗?

android - 按下主页按钮并重新打开应用程序后 MediaPlayer 出现 NullPointerException

mysql - 引发 MysqlError; raise Mysql::Error 都有效,这是怎么发生的?

android - 如何在android中动态声明n个EditTexts?

android - 像 Google Play 一样搜索编辑文本