0

在下面的代码中仍然出现java.lang.StringIndexOutOfBoundsException: index=0 length=0错误if (result.charAt(0) =='Y')

还有其他更好的方法来检查空字符串if (result == null || result.isEmpty())吗?由于某种原因,空字符串“result”没有被拾取或activity.finish();没有执行,并且下一个 if 语句被执行了?请提供任何帮助。

@Override
    protected void onPostExecute(String result) {

        //check if result null or empty
        if (result == null || result.isEmpty()) {

            Toast.makeText(context, "Could Not Check For Updates, Please Try Again. Closing Application.", Toast.LENGTH_LONG).show();
            activity.finish();
        }           
        //update available
        if (result.charAt(0) =='Y') {

            UpdateAlert();
        }
        //no update available
        else if (result.charAt(0) =='N') {

            Toast.makeText(context, "No Updates Available", Toast.LENGTH_SHORT).show();             
        }
        else {

            Toast.makeText(context, "Could Not Check For Updates, Please Try Again. Closing Application : " + result, Toast.LENGTH_LONG).show();
            activity.finish();
        }   
4

2 回答 2

0

一旦遇到无效结果,请尝试从该方法返回,如下所示

//check if result null or empty         
           if (result == null || result.isEmpty()) {              
                Toast.makeText(context, "Could Not Check For Updates, Please Try Again. Closing Application.", Toast.LENGTH_LONG).show();             
                activity.finish();         
                return;
           }  
于 2012-08-08T13:56:53.173 回答
0
   if (result == null || result.trim().length()==0) {
         Toast.makeText(context, "Could Not Check For Updates, Please Try Again. Closing  Application.", Toast.LENGTH_LONG).show();
            activity.finish();
        }           
        //update available
        else if (result.charAt(0) =='Y') {
        ^^^^ 
            UpdateAlert();
        }
于 2012-08-08T13:57:07.477 回答