禁用提示时 TextInputLayout 空指针异常

TextInputLayout nullpointer exception while disabling hint

我正在动态添加 TextInputLayout,它在设置禁用提示时崩溃了。以下是代码。

private TextInputLayout createNewTextInputLayout() {
    final LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    final EditText editText = new EditText(this);
    editText.setLayoutParams(lParams);
    editText.setHint(getString(R.string.lbl_email_sms_notification));
    editText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorGrayListHeaderFontColor));
    editText.setHintTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorGrayListHeaderFontColor));

    TextInputLayout txtInputLayout = new TextInputLayout(this);
    lParams.setMargins(0, 25, 0, 0);
    txtInputLayout.setLayoutParams(lParams);
    txtInputLayout.setHintEnabled(false);
    txtInputLayout.addView(editText);

    return txtInputLayout;
}

日志如下:

Process: com.test, PID: 18328 java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.test/com.test.view.activity.SelectAlertReviewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.EditText.getHint()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access0(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.CharSequence android.widget.EditText.getHint()' on a null object reference
at android.support.design.widget.TextInputLayout.setHintEnabled(TextInputLayout.java:376)
at com.test.view.activity.SelectAlertReviewActivity.createNewTextInputLayout(SelectAlertReviewActivity.java:291)
at com.test.view.activity.SelectAlertReviewActivity.initializeGUI(SelectAlertReviewActivity.java:144)
at com.test.view.activity.SelectAlertReviewActivity.onCreate(SelectAlertReviewActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)

at android.app.ActivityThread.access0(ActivityThread.java:172) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5832) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

出现此问题是因为 TextInputLayout 中的 mEditText 等于 null。您必须在调用 setHintEnabled(boolean).

之前添加视图

变化:

TextInputLayout txtInputLayout = new TextInputLayout(this);
lParams.setMargins(0, 25, 0, 0);
txtInputLayout.setLayoutParams(lParams);
txtInputLayout.setHintEnabled(false);    // here you have null pointer
txtInputLayout.addView(editText);

至:

TextInputLayout txtInputLayout = new TextInputLayout(this);
lParams.setMargins(0, 25, 0, 0);
txtInputLayout.setLayoutParams(lParams);
txtInputLayout.addView(editText);  // add view as first step
txtInputLayout.setHintEnabled(false); // disable hint after setting editText