Illya Bublyk
Illya Bublyk

Reputation: 722

Class cannot be cast to ParseObject

I am using Parse.com in my project. I want to create my own entity class extends Parse Object but I have a java.lang.ClassCastException: com.parse.ParseObject cannot be cast to com.james.strongpeopleapp.Recipe

@ParseClassName("Recipe")
public class Recipe extends ParseObject {

    public Recipe() {
    }

    public String getRecipeName(){
        return getString("RecipeName");
    }

Registering a subclass

public class ParseApp extends Application {

    @Override
    public void onCreate() {

        ParseObject.registerSubclass(Recipe.class);
        Parse.initialize(this, "XXXXXX", "XXXXXXX");

    }
}

Exception take place in getItemView() in adapter

 public class MainParseAdapter extends ParseQueryAdapter<Recipe> {

private LayoutInflater layoutInflater;

public MainParseAdapter(Context context) {

    super(context, new QueryFactory<Recipe>() {
        @Override
        public ParseQuery<Recipe> create() {
            ParseQuery query = new ParseQuery("OwnRecipeBook");
            return query;
        }
    });

    layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getItemView(Recipe recipe, View v, ViewGroup parent) {

    if(v == null){
        v = layoutInflater.inflate(R.layout.recipe_item, parent, false);
    }
    super.getItemView(recipe, v, parent);

    TextView mName = (TextView) v.findViewById(R.id.recipe_tw);
    mName.setText(recipe.getRecipeName());

    ParseImageView mImage = (ParseImageView) v.findViewById(R.id.recipe_imageView);
    mImage.setParseFile(recipe.getRecipeImage());
    mImage.loadInBackground();

    return v;
}

public String getItemIdFromTable(int position){
    return getItem(position).getObjectId();
}

}

Upvotes: 2

Views: 2551

Answers (1)

async
async

Reputation: 1537

Compare this:

@ParseClassName("Recipe")

To this:

ParseQuery query = new ParseQuery("OwnRecipeBook");

There's a type mismatch. Try to use the same name. Those types may look obviously identical to you, but Parse needs to know precisely, unambiguously what you're trying to do.

Upvotes: 5

Related Questions