我动态添加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.access$900(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.access$900(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)
答案 0 :(得分:1)
发生此问题是因为mEditText
中的TextInputLayout
等于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