OSDN Git Service

Fix crash in EditText#getText
authorClara Bayarri <clarabayarri@google.com>
Thu, 18 Jan 2018 12:36:00 +0000 (12:36 +0000)
committerClara Bayarri <clarabayarri@google.com>
Thu, 18 Jan 2018 12:36:00 +0000 (12:36 +0000)
The fact that View's constructor may call other methods before the
TextView constructor has a chance to initialize values means that
getText() may return null in the rare case where it is called
before the constructor.

Protect getText() from running into errors when the text is null,
as it just means the value is not ready yet.

Bug: 71638589
Test: CTS in topic
Change-Id: Ie11233416385353f78e3ee27fb0d3ec9b671512f

core/java/android/widget/EditText.java

index 336c20c..728824c 100644 (file)
@@ -106,6 +106,10 @@ public class EditText extends TextView {
     @Override
     public Editable getText() {
         CharSequence text = super.getText();
+        // This can only happen during construction.
+        if (text == null) {
+            return null;
+        }
         if (text instanceof Editable) {
             return (Editable) super.getText();
         }