android 中的配置更改时如何解决 edittext 中的 getLayout() 方法变为 null 时的问题

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

我正在尝试使用 textwatcher 界面从其 addTextChangeListener 中的多行 Edittext 获取单行。

我想用这个 EditText 的唯一第一行更新另一个 EditText。一切正常,除了旋转设备时,activity 和整个应用程序终止

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) {

        }
    });
}

}

但是当旋转屏幕时发生配置更改时,会出现以下错误,是否有任何其他方法可以从多行 Edittext 中获取单行文本,如果可以的话,将不胜感激,提前谢谢

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.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) 

EditText getLayout() 方法在设备旋转时可以为空。 如此安全的方法首先进行空检查。

更新方法为:

@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);
        }

    }