-1

I am creating a keyboard but there is some error in local variable usage.

 private void updateCandidateText(){
        try{

            ExtractedText r= getCurrentInputConnection().getExtractedText(new ExtractedTextRequest(),InputConnection.GET_EXTRACTED_TEXT_MONITOR);
            String strbeforeCursor="";
            String strafterCursor ="";
                strbeforeCursor = getCurrentInputConnection().getTextBeforeCursor(1000000000, 0).toString();
            strafterCursor = getCurrentInputConnection().getTextAfterCursor(1000000000, 0).toString();
            String str = strbeforeCursor + "|"+strafterCursor;
            if(mTamilPreviewView != null)
                mTamilPreviewView.update(str, strbeforeCursor.length());

            mTamilPreviewView.update(r.text.toString() , 0);
        }
        catch (Exception e) {
            Log.e("t", "errr", e);
        }    
    }
user2060383
  • 979
  • 1
  • 14
  • 32

1 Answers1

0

You test if mTamilPreviewView != null to call

mTamilPreviewView.update(str, strbeforeCursor.length());

but even if it's null, you'll do

mTamilPreviewView.update(r.text.toString() , 0);

and you'll get a NullPointerException. Is it really what you want to do? Don't you mean

if (mTamilPreviewView != null) {
    mTamilPreviewView.update(str, strbeforeCursor.length());
    mTamilPreviewView.update(r.text.toString() , 0);
}

Moreover, you initialize strbeforeCursor and strafterCursor with an empty string, and you give them other values at the next lines. You could simply remove

String strbeforeCursor="";
String strafterCursor ="";

and do

String strbeforeCursor = getCurrentInputConnection().getTextBeforeCursor(1000000000, 0).toString();
String strafterCursor = getCurrentInputConnection().getTextAfterCursor(1000000000, 0).toString();
Benoît Guédas
  • 801
  • 7
  • 25