2017-08-26 19 views
10

mam prosty formularz adres takiego:Android - próba wywołania metody wirtualne 'void android.view.View.getBoundsOnScreen (android.graphics.Rect)' na null odniesienia obiektu

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> 

Jak widać na java, mAddressEditing zmienna jest odbierany z zamiarem, kiedy its not null to znaczy użytkownik chce zmienić swój adres.

Wszystkie pola są wypełnione zgodnie z oczekiwaniami, jednak kiedy dotknąć na tekst edycji, aby zmienić jej wartość to zawiesza ...

awarii:

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) 

nie mam pojęcia, dlaczego to upaść.

Kroki do odtworzenia: 1. Edytowanie adres z poprzedniego ekranu przekazując obiekt jako parametr na adres zamiarem 2. kliknij na tekst edycji, aby zmienić jego wartość 3. Błąd!

+1

https://stackoverflow.com/questions/45840856/android-8-0-oreo-crash-on-focusing-textinputedittext –

+0

Dzięki Mike M.! Znalazłem rozwiązanie z twoim linkiem! Dodam komentarz, jeśli ktoś inny ma taki sam problem. –

+0

Zrobię to ... Dzięki –

Odpowiedz

22

Dodanie tego kodu na każdej edycji tekstu rozwiązać mój problem:

android:importantForAutofill="noExcludeDescendants" 
+0

to tylko atrybut API 26 – JPM

+1

Hi Douglas, to również rozwiązało mój problem, ale nie do końca rozumiem * dlaczego * tak jest. Dlaczego tak trzeba to ustawić? EDYCJA: Nieważne, stwierdziłem, że jest to błąd w/systemie Android https://issuetracker.google.com/issues/62834931 –

+1

Użytkownik może nadal ręcznie wybrać Autowypełnianie za pomocą tego zestawu przez długie naciśnięcie pola i wybranie przepełnienia , tam jest autouzupełnianie. Tak więc nadal można doświadczyć awarii. Zobacz ten stos, aby znaleźć rozwiązanie. https://stackoverflow.com/questions/45840856/android-8-0-oreo-crash-on-focusing-textinputedittext – Azethoth

3

Problem jest znany bug Android. Z sugestii modułu do zgłaszania problemów dotyczących Google wynika, że ​​ustawienie wskazówki dla TextInputEditText powoduje awarię. Ustawienie wskazówki tylko w TextInputLayout naprawia awarię.

Ten problem występuje tylko wtedy, gdy wskazówka jest ustawiona na zagnieżdżonego EditText wewnątrz TextInputLayout. Rozwiązałem go, ustawiając podpowiedź na TextInputLayout.

https://issuetracker.google.com/issues/62834931 Komentarz # 28

+1

Preferuję to rozwiązanie zamiast Androida: importantForAutofill = "noExcludeDescendants" , który jest tylko atrybutem API 26 – user2402046

1

Ustaw wskazówkę na TextInputLayout zamiast zagnieżdżonych EditText. To się nie zawiesi.

<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> 
+0

Pracowałem dla mnie w OREO. – Ajji