我正在尝试使用textwatcher接口从addTextChangeListener中的多行Edittext中获取一行。
我想用另一个EditText的第一行更新另一个EditText。它的工作正常,除非设备旋转,活动和整个应用程序终止
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$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)
答案 0 :(得分:0)
EditText getLayout()方法可以为null。 这样安全的方法首先进行空检查。
更新方法:
@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);
}
}