Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) The Model-View-Controller Pattern Creating the Story

James Richardson
James Richardson
2,496 Points

Why is my setChioice1 and setChoice2 not recognised? I have spent days trying to figure it out please help!

setChoice1 and setChoice2 are red and the story.java as a red lie underneath here is my code:

package com.jamesrichardson.interactiveadventurestory.model;

import com.jamesrichardson.interactiveadventurestory.R;
import com.jamesrichardson.interactiveadventurestory.model.Choice;

public class Story {

    private Page[] mPages;

    public Story()
    {
        mPages = new Page[7];
        mPages[0] = new Page();
        mPages[0].setImageId(R.mipmap.page0);
        mPages[0].setText("test");
        mPages[0].setChoice1 (new Choice());
        mPages[0].setChoice2 (new Choice());


    }
}

PLEASE HELP!

Harry James
Harry James
14,780 Points

Glad to hear you got this fixed now James!

If you could mark Steve's answer as the Best Answer then it will credit him and also let other forum users know that the question has been answered :)

11 Answers

Hi James,

Boris is correct. Your Page class should have the methods setChoice1 and setChoice2 in it.

You must have missed implementing them somehow.

I'd suggest downloading the project files from the previous video, as that puts you at the start of this one from a code perspective. With a bit of tinkering with the package names etc. you should be good to go.

Good luck!

Steve.

Boris Musatov
Boris Musatov
2,535 Points

Hi James!
Your Page class has no setChoice1 and setChoice2 methods.

Harry James
Harry James
14,780 Points

Hey James!

It looks like you have a space here:

setChoice2 (new Choice());
          ^

on SetChoice1 and setChoice2. If you get rid of the space, your error should go away.

Hope it helps and, if you have any more problems, give me a shout :)

James Richardson
James Richardson
2,496 Points

Hi Harry,

I tried it and I still have the error with this: Cannot resolve method 'setChoice1(com.jamesrichardson.interactiveadventurestory.model.Choice)'

Thank you for your time .

Best Wishes

J

Harry James
Harry James
14,780 Points

Ok. As long as your Choice method is public and has no errors, the class should be accessible.

Try Cleaning your project. To do this, click on the Build tab and then Clean Project and wait for the Clean to finish:


If you are still having issues, try Invalidating Android Studio's caches. To do this, click on the File tab then Invalidate Caches / Restart and press Invalidate and Restart:


If still after this you are having problems, copy your Choice.java class here and I'll see if there's any problems with it.

James Richardson
James Richardson
2,496 Points

Hello again, it didn't work. Thought that it might. Sat here with all fingers crossed (and toes). Here is my Choice.java:

package com.jamesrichardson.interactiveadventurestory.model;

/**
 * Created by James on 20/03/2015.
 */
public class Choice {
    private String mText;
    private int mNextPage;
    private Choice mChoice1;
    private Choice mChoice2;
    private int mImageId;

    public String getText() {
        return mText;
    }

    public void setText(String text) {
        mText = text;
    }

    public int getNextPage() {
        return mNextPage;
    }

    public void setNextPage(int nextPage) {
        mNextPage = nextPage;
    }

    public Choice getChoice1() {
        return mChoice1;
    }

    public void setChoice1(Choice choice1) {
        mChoice1 = choice1;
    }

    public Choice getChoice2() {
        return mChoice2;
    }

    public void setChoice2(Choice choice2) {
        mChoice2 = choice2;
    }

    public int getImageId() {
        return mImageId;
    }

    public void setImageId(int imageId) {
        mImageId = imageId;
    }
}

Hi James,

Sorry, but I have no idea what you're trying to get through here as I have not done that course. Apologies!

Your question suggests you have an issue with setChoice1 and setChoice2. Those are both being called as methods of the first element of the mPage array. Does the class Page implement methods called setChoice1 and setChoice2?

Following that, you're creating two new instances of a class called Choice. What does that class code look like?

A good way round this is the upload your code into Github so we can see exactly what we're working with. Once we've got your code we can work out what's not working.

Cheers,

Steve.

James Richardson
James Richardson
2,496 Points

OK Will go to GitHub now. Be right back.

Thanks.

Perfect; thanks.

I'm OnlySteveH on there (and Twitter), if you want to invite me to view.

Steve.

James Richardson
James Richardson
2,496 Points

https://github.com/jimmystrings1/Treehouse/issues I don;t know how to use GitHub but here is the link to the files.

Many thanks

James

Thanks; I'll have a look at that tomorrow. I have work in the morning and need to get some sleep.

Steve.

James Richardson
James Richardson
2,496 Points

Hi Steve I have added you. I have posted them under alerts. Many thanks. Night night.

I'll have a look tomorrow ... thanks!

Steve.

James Richardson
James Richardson
2,496 Points

Thanks for taking the time Steve really appreciate it!

No problem!

Steve.

Hi All,

I have absolutely the same problem as James Richardson. Would you please share what was the resolution of the problem in the code with setChoice? I tried what Harry James suggested but it did not help.

Hi Adilet

The problem with this initial query was that the Page class had no setChoice methods implemented.

It's probably best you share your code if you are having difficulties, after checking you've got setChoice1 and setChoice2 implemented within the Page class.

Cheers,

Steve.

Hi Steve,

Thank you very much! I have already fixed the problem. Just did not pay attention.

Glad you got it fixed!

Any other problems, just give me a shout on here, or on Twitter @OnlySteveH - I'll hear you!

Steve.