savishy / TestApp

My attempt to build a simple app that uses a database
0 stars 0 forks source link

Crash when selecting ingredient #7

Closed savishy closed 8 years ago

savishy commented 8 years ago

Commit d65c6517a5d59eb73e0b5c5ac393373e1687ffb6:

This was written to fix #1 . However it crashes when an ingredient is selected.

java.lang.ClassCastException: java.lang.String cannot be cast to com.vish.testapp.Ingredient
                                                                  at com.vish.testapp.IngredientsCompletionView.getViewForObject(IngredientsCompletionView.java:24)
                                                                  at com.tokenautocomplete.TokenCompleteTextView.buildSpanForObject(TokenCompleteTextView.java:755)
                                                                  at com.tokenautocomplete.TokenCompleteTextView.replaceText(TokenCompleteTextView.java:767)
savishy commented 8 years ago

I wonder if the crash is happening in this line (inside TokenAutoComplete)

@SuppressWarnings("unchecked cast")
@Override
protected CharSequence convertSelectionToString(Object object) {
    selectedObject = (T) object;

It looks like a cast to the generic type (T) which in my case is Ingredient.

EDIT: No, it was a cast from String to Ingredient, not the other way around.

It looks like this is something related to http://www.studytonight.com/java/serialization-and-deserialization.php

savishy commented 8 years ago

Figured it out. (facepalm)

background

The TokenCompletionTextView (see source) needs to be initialized inside an Activity (obviously).

In my code, I am doing the init as follows.

The ArrayAdapter is initialized as String but it should be initialized as ArrayAdapter<Ingredient> for this thing to work.

savishy commented 8 years ago

This issue is verified fixed.