Search code examples
javaandroidandroid-viewandroid-configchanges

How to solve when getLayout() method in edittext becomes null, when configuration changes in android


I am trying to get a single line from a multi line Edittext in its addTextChangeListener, using the textwatcher interface.

I wanted to update another EditText with the only first line of this EditText. Its all working fine, except when the device is rotated, the activity and the whole application get terminated

public class CreateNoteActivity extends AppCompatActivity {

private EditText etNote, etNoteTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_note);
    setTitle("Write Note");

    etNoteTitle = findViewById(R.id.etNoteTitle);
    etNote = findViewById(R.id.etNote);
    etNote.requestFocus();

    etNote.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            int start = etNote.getLayout().getLineStart(0);
            int end = etNote.getLayout().getLineEnd(0);
            String title = etNote.getText().subSequence(start, end).toString();
            etNoteTitle.setText(title);




        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}

}

but when configuration changes that is when rotated the screen the following error occurs, is there any other way to get a single line of text from a multiline Edittext, if so it would be really appreciated thank you in advance

1-21 16:47:28.792 12534-12534/com.example.haadee.noteitdown E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.haadee.noteitdown, PID: 12534
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.haadee.noteitdown/com.example.haadee.noteitdown.ui.CreateNoteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineStart(int)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
                                                                               at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4560)
                                                                               at android.app.ActivityThread.-wrap19(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6165)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineStart(int)' on a null object reference
                                                                               at com.example.haadee.noteitdown.ui.CreateNoteActivity$1.onTextChanged(CreateNoteActivity.java:39)
                                                                               at android.widget.TextView.sendOnTextChanged(TextView.java:8231)
                                                                               at android.widget.TextView.setText(TextView.java:4512)
                                                                               at android.widget.TextView.setText(TextView.java:4366)
                                                                               at android.widget.EditText.setText(EditText.java:89)
                                                                               at android.widget.TextView.setText(TextView.java:4341)
                                                                               at android.widget.TextView.onRestoreInstanceState(TextView.java:4232)
                                                                               at android.view.View.dispatchRestoreInstanceState(View.java:15767)
                                                                               at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3240)
                                                                               at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3240)
                                                                               at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3240)
                                                                               at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3240)
                                                                               at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3240)
                                                                               at android.view.View.restoreHierarchyState(View.java:15745)
                                                                               at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2106)
                                                                               at android.app.Activity.onRestoreInstanceState(Activity.java:1051)
                                                                               at android.app.Activity.performRestoreInstanceState(Activity.java:1006)
                                                                               at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1196)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
                                                                               at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4560) 
                                                                               at android.app.ActivityThread.-wrap19(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6165) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

Solution

  • EditText getLayout() method can be null when device rotated. so safe way to do null check first.

    Update method to:

    @Override
        public void onTextChanged(CharSequence s, int i, int before, int count) {
            // do null check here
            if (etNote.getLayout() != null) {
                int start = etNote.getLayout().getLineStart(0);
                int end = etNote.getLayout().getLineEnd(0);
                String title = etNote.getText().subSequence(start, end).toString();
                etNoteTitle.setText(title);
            }
    
        }