【问题标题】:Full proof check for java.lang.StringIndexOutOfBoundsException in AndroidAndroid 中 java.lang.StringIndexOutOfBoundsException 的完整证明检查
【发布时间】:2012-08-08 13:51:31
【问题描述】:

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

还有其他更好的方法来检查 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();
        }   

【问题讨论】:

  • 尝试接受适合您的答案,以便为您的问题提供更多的帮助。

标签: android null android-asynctask indexoutofboundsexception


【解决方案1】:
   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();
        }

【讨论】:

    【解决方案2】:

    遇到如下修改的无效结果时尝试从方法返回

    //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;
               }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多