J'ai un formulaire d'adresse simple comme ceci:
Java:
public class NewAddressActivity extends AppCompatActivity {
private TextInputLayout mStreetLayout;
private TextInputLayout mNumberLayout;
private TextInputLayout mNeighborhoodLayout;
private TextInputLayout mCityLayout;
private TextInputLayout mStateLayout;
private TextInputLayout mCepLayout;
private TextInputLayout mPhoneLayout;
private EditText mStreetText;
private EditText mNumberText;
private EditText mComplementText;
private EditText mNeighborhoodText;
private EditText mCityText;
private EditText mStateText;
private EditText mCepText;
private EditText mPhoneText;
private Address mAddressEditing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_address);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
mStreetLayout = findViewById(R.id.street_layout);
mNumberLayout = findViewById(R.id.number_layout);
mNeighborhoodLayout = findViewById(R.id.neighborhood_layout);
mCityLayout = findViewById(R.id.city_layout);
mStateLayout = findViewById(R.id.state_layout);
mCepLayout = findViewById(R.id.cep_layout);
mPhoneLayout = findViewById(R.id.phone_layout);
mStreetText = findViewById(R.id.street_text);
mNumberText = findViewById(R.id.number_text);
mComplementText = findViewById(R.id.complement_text);
mNeighborhoodText = findViewById(R.id.neighborhood_text);
mCityText = findViewById(R.id.city_text);
mStateText = findViewById(R.id.state_text);
mCepText = findViewById(R.id.cep_text);
mPhoneText = findViewById(R.id.phone_text);
mAddressEditing = getIntent().getParcelableExtra(AppConstants.ADDRESS_EXTRA);
if (mAddressEditing != null) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
if (actionBar != null) {
actionBar.setTitle(R.string.edit_address);
}
mStreetText.setText(mAddressEditing.getStreet());
mNumberText.setText(mAddressEditing.getNumber());
mComplementText.setText(mAddressEditing.getComplement());
mNeighborhoodText.setText(mAddressEditing.getNeighborhood());
mCityText.setText(mAddressEditing.getCity());
mStateText.setText(mAddressEditing.getState());
mCepText.setText(mAddressEditing.getCep());
mPhoneText.setText(mAddressEditing.getPhone());
mStreetText.setSelection(mAddressEditing.getStreet().length());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_new_address, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Android.R.id.home:
finish();
return true;
case R.id.action_save:
save();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressLint("StaticFieldLeak")
private void save() {
String street = mStreetText.getText().toString().trim();
String number = mNumberText.getText().toString().trim();
String complement = mComplementText.getText().toString().trim();
String neighborhood = mNeighborhoodText.getText().toString().trim();
String city = mCityText.getText().toString().trim();
String state = mStateText.getText().toString().trim();
String cep = mCepText.getText().toString().trim();
String phone = mPhoneText.getText().toString().trim();
boolean hasError = false;
if (TextUtils.isEmpty(street)) {
hasError = true;
mStreetLayout.setErrorEnabled(true);
mStreetLayout.setError(getString(R.string.fill_the_field));
}
if (TextUtils.isEmpty(number)) {
hasError = true;
mNumberLayout.setErrorEnabled(true);
mNumberLayout.setError(getString(R.string.fill_the_field));
}
if (TextUtils.isEmpty(neighborhood)) {
hasError = true;
mNeighborhoodLayout.setErrorEnabled(true);
mNeighborhoodLayout.setError(getString(R.string.fill_the_field));
}
if (TextUtils.isEmpty(city)) {
hasError = true;
mCityLayout.setErrorEnabled(true);
mCityLayout.setError(getString(R.string.fill_the_field));
}
if (TextUtils.isEmpty(state)) {
hasError = true;
mStateLayout.setErrorEnabled(true);
mStateLayout.setError(getString(R.string.fill_the_field));
}
if (TextUtils.isEmpty(cep)) {
hasError = true;
mCepLayout.setErrorEnabled(true);
mCepLayout.setError(getString(R.string.fill_the_field));
}
if (TextUtils.isEmpty(phone)) {
hasError = true;
mPhoneLayout.setErrorEnabled(true);
mPhoneLayout.setError(getString(R.string.fill_the_field));
}
if (hasError) {
return;
}
final Address address = new Address();
if (mAddressEditing != null) {
mAddressEditing.setStreet(street);
mAddressEditing.setNumber(number);
mAddressEditing.setComplement(complement);
mAddressEditing.setNeighborhood(neighborhood);
mAddressEditing.setCity(city);
mAddressEditing.setState(state);
mAddressEditing.setCep(cep);
mAddressEditing.setPhone(phone);
} else {
address.setStreet(street);
address.setNumber(number);
address.setComplement(complement);
address.setNeighborhood(neighborhood);
address.setCity(city);
address.setState(state);
address.setCep(cep);
address.setPhone(phone);
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
if (mAddressEditing != null) {
MainApplication.getInstance().getAddressDao().update(mAddressEditing);
} else {
MainApplication.getInstance().getAddressDao().insert(address);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(NewAddressActivity.this,
mAddressEditing != null ? R.string.address_edited_successfully :
R.string.address_created_successfully, Toast.LENGTH_SHORT).show();
setResult(Activity.RESULT_OK);
finish();
}
}.execute();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<Android.support.constraint.ConstraintLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content">
<Android.support.design.widget.TextInputLayout
Android:id="@+id/street_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent">
<EditText
Android:id="@+id/street_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/street"
Android:imeOptions="actionNext"
Android:inputType="textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/number_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/street_layout">
<EditText
Android:id="@+id/number_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/number"
Android:imeOptions="actionNext"
Android:inputType="textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/complement_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/number_layout">
<EditText
Android:id="@+id/complement_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/complement"
Android:imeOptions="actionNext"
Android:inputType="textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/neighborhood_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/complement_layout">
<EditText
Android:id="@+id/neighborhood_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/neighborhood"
Android:imeOptions="actionNext"
Android:inputType="textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/city_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/neighborhood_layout">
<EditText
Android:id="@+id/city_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/city"
Android:imeOptions="actionNext"
Android:inputType="textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/state_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/city_layout">
<EditText
Android:id="@+id/state_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/state"
Android:imeOptions="actionNext"
Android:inputType="textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/cep_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/state_layout">
<EditText
Android:id="@+id/cep_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/cep"
Android:imeOptions="actionNext"
Android:inputType="number"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
<Android.support.design.widget.TextInputLayout
Android:id="@+id/phone_layout"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginEnd="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginStart="50dp"
Android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/cep_layout">
<EditText
Android:id="@+id/phone_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="@string/phone"
Android:imeOptions="actionDone"
Android:inputType="phone|textCapSentences"
Android:maxLines="1"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>
</Android.support.constraint.ConstraintLayout>
</ScrollView>
Comme vous pouvez le voir sur Java, la variable mAddressEditing
est reçue d’intention. Quand elle n’est pas nulle, cela signifie que l’utilisateur veut modifier son adresse.
Tous les champs sont remplis comme prévu. Toutefois, lorsque je tape sur un texte de texte pour en modifier la valeur, il se bloque ...
Crash:
FATAL EXCEPTION: main
Process: br.com.fornaro.armariovirtual, PID: 5540
Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.view.View.getBoundsOnScreen(Android.graphics.Rect)' on a null object reference at Android.app.assist.AssistStructure$WindowNode.<init>(AssistStructure.Java:484)
at Android.app.assist.AssistStructure.<init>(AssistStructure.Java:1908)
at Android.app.ActivityThread.handleRequestAssistContextExtras(ActivityThread.Java:3035)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1807)
at Android.os.Handler.dispatchMessage(Handler.Java:105)
at Android.os.Looper.loop(Looper.Java:164)
at Android.app.ActivityThread.main(ActivityThread.Java:6541)
at Java.lang.reflect.Method.invoke(Native Method)
at com.Android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.Java:240)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:767)
Je ne sais pas pourquoi il s'écrase.
Étapes à suivre pour la reproduction: 1. Modifiez une adresse à partir de l'écran précédent en transmettant l'objet Adresse en tant que paramètre d'intention. 2. Cliquez sur un texte à modifier pour en modifier la valeur 3. Crash!
L'ajout de ce code sur chaque texte modifié a résolu mon problème:
Android:importantForAutofill="noExcludeDescendants"
Le problème est connu: Android bug. D'après les suggestions de Google pour le suivi des problèmes, définir l'indicateur sur TextInputEditText semble provoquer le blocage. Définir l'indicateur uniquement sur TextInputLayout corrige le blocage.
Ce problème ne se produit que si l'indicateur est défini sur le texte d'édition imbriqué dans TextInputLayout. Je l'ai résolu en définissant l'indicateur sur TextInputLayout.
https://issuetracker.google.com/issues/62834931 Commentaire n ° 28
Définissez l'indicateur sur TextInputLayout au lieu de EditText imbriqué. Il ne va pas s'écraser.
<Android.support.design.widget.TextInputLayout
Android:id="@+id/til1"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="Phone Number">
<EditText
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:id="@+id/login_phone"
Android:inputType="number"
Android:singleLine="true" />
</Android.support.design.widget.TextInputLayout>