0

以下のコードでjava.lang.StringIndexOutOfBoundsException: index=0 length=0エラーが引き続き発生するif (result.charAt(0) =='Y')

でヌル文字列をチェックする他の良い方法はありますif (result == null || result.isEmpty())か? 何らかの理由で空文字列「結果」が取得され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 に答える