Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android应用程序崩溃,尽管捕获了异常_Android_Exception - Fatal编程技术网

Android应用程序崩溃,尽管捕获了异常

Android应用程序崩溃,尽管捕获了异常,android,exception,Android,Exception,我正在编写一个简单的应用程序,它允许用户输入他们的收入并扣除税款,然后将金额保存在一个文件中以供将来参考。每当我尝试输入金额时,就会收到一条警告,说明应用程序意外停止。这是我的密码: public void onClick(View v) { // TODO Auto-generated method stub try { if (preTax !=null){ Double incomeAmount = Double.

我正在编写一个简单的应用程序,它允许用户输入他们的收入并扣除税款,然后将金额保存在一个文件中以供将来参考。每当我尝试输入金额时,就会收到一条警告,说明应用程序意外停止。这是我的密码:

public void onClick(View v) {
    // TODO Auto-generated method stub
    try {

        if (preTax !=null){         
            Double incomeAmount = Double.parseDouble(preTax.getText().toString());
            incomeAmount =- (20 *100)/incomeAmount;     
            Double incomeRounded = Round(incomeAmount);
            Toast.makeText(null, "Your income minus tax = "+incomeRounded, Toast.LENGTH_LONG).show();


            FileOutputStream fos = openFileOutput("income", Context.MODE_PRIVATE);
            fos.write("1000".getBytes());
            fos.close();
        }
        else {

            Double incomeAmount = Double.parseDouble(postTax.getText().toString());
            Double incomeRounded = Round(incomeAmount);
            Toast.makeText(null, "Your income is: "+ incomeRounded, Toast.LENGTH_LONG).show();


            FileOutputStream fos = openFileOutput("income", Context.MODE_PRIVATE);
            fos.write("1000".getBytes());
            fos.close();
        }

    } catch (Exception e){
        Toast.makeText(null, "Please fill in the catagories" + e, Toast.LENGTH_LONG).show();
    }
}

这个问题发生在添加fileoutstream之前,所以我知道这不是问题所在,但我不清楚是什么问题。无论EditText是否为空,程序都会崩溃。try/catch肯定会捕获任何错误吗

为上下文传入null并没有什么帮助。你的应用程序正在爆炸,被抓到,然后又再次爆炸。

为上下文输入null并没有什么帮助。你的应用程序正在爆炸,被抓到,然后再次爆炸。

Toast.makeText(null,“请填写分类”+e,Toast.LENGTH_LONG)。show()

应该是

Toast.makeText(v.getContext(),“请填写分类”+e,Toast.LENGTH_LONG.show()

您不能在中传递
null
,因为上下文必须有效。

Toast.makeText(null,“请填写分类”+e,Toast.LENGTH_LONG)。show()

应该是

Toast.makeText(v.getContext(),“请填写分类”+e,Toast.LENGTH_LONG.show()

不能在中传递
null
,因为上下文需要有效