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) Finishing the User Interface Loading Additional Pages

Jonathan Erlandsson
Jonathan Erlandsson
2,361 Points

I can't press the choice Button when I am testing the app

I can't press the choice button nothing happen it do not give any respond.

Here is my Code

<code>

package se.instagibbed.interactivestory.ui;

import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import se.instagibbed.interactivestory.R; import se.instagibbed.interactivestory.model.Page; import se.instagibbed.interactivestory.model.Story;

public class StoryActivity extends ActionBarActivity {

public static final String TAG = StoryActivity.class.getSimpleName();

private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextVeiw;
private Button mChoice1;
private Button mChoice2;
private String mName;
private Page mCurrentPage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    Intent intent = getIntent();
    mName = intent.getStringExtra(getString(R.string.key_name));

    if (mName == null) {
        mName = "Friend";
    }
    Log.d(TAG, mName);

    mImageView =(ImageView)findViewById(R.id.storyImageView);
    mTextVeiw =(TextView)findViewById(R.id.storyTextView);
    mChoice1 =(Button)findViewById(R.id.choiceButton1);
    mChoice2 =(Button)findViewById(R.id.choiceButton2);

    loadPage(0);
}

private void loadPage(int choice) {
    mCurrentPage = mStory.getPage(choice);
    Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
    mImageView.setImageDrawable((drawable));

    String pageText = mCurrentPage.getText();
    // add the name if placeholder incluide, won't add if no placeholdr
    pageText = String.format(pageText, mName);

    mTextVeiw.setText(pageText);

    mChoice1.setText(mCurrentPage.getChoice1().getText());
    mChoice2.setText(mCurrentPage.getChoice2().getText());

    mChoice1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = mCurrentPage.getChoice1().getNextPage();
            loadPage(nextPage);
        }
    });
    mChoice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = mCurrentPage.getChoice2().getNextPage();
            loadPage(nextPage);
        }
    });

}

}

</code>

And my XML Code

<code> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="se.instagibbed.interactivestory.ui.StoryActivity" android:background="@android:color/white">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/storyImageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/page0"
    android:scaleType="fitXY"
    android:adjustViewBounds="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Continue Home to Earth"
    android:id="@+id/choiceButton1"
    android:layout_above="@+id/choiceButton2"
    android:layout_alignLeft="@+id/choiceButton2"
    android:layout_alignStart="@+id/choiceButton2"
    android:background="@android:color/white"
    android:textColor="#ff3a8aec" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Stop and investigate"
    android:id="@+id/choiceButton2"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@android:color/white"
    android:textColor="#FF3A8AEC" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low."
    android:id="@+id/storyTextView"
    android:layout_below="@+id/storyImageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="30dp"
    android:paddingTop="15dp"
    android:paddingRight="30dp"
    android:lineSpacingMultiplier="1.2" />

</RelativeLayout> </code>

And i can't understand where i have done something wrong.

4 Answers

Jonathan Erlandsson
Jonathan Erlandsson
2,361 Points

I know what the problem with my code was I did find it. And that was under Choice.java I hade typed "nextPage = nextPage;" instead of "mNextPage = nextPage;"

maybe you have done the samething

Jeremiah Shore
Jeremiah Shore
31,168 Points

I copied and pasted your code and I could still compile and run without issues.

I did notice that you have an extra () in the line

mImageView.setImageDrawable((drawable));

but that didnt cause any problems when I ran the app. I also noticed you have a typo in your TextView member variable name (mTextVeiw), but yet again should have no effect because you use it in all places.

What version of Android Studio are you running? Try doing Build > Clean Project, and perhaps restarting Anroid Studio. Are you running on an Emulator or a real device?

**also when posting android code you can use the word 'java' after the first set of backticks for colored markdown

Jonathan Erlandsson
Jonathan Erlandsson
2,361 Points

I running on a real device have tried to clean the projekt and restart android studio and still not work my device is samsung galaxy note 3. The first Button work white you gona type in your name bit the secondpage want work white i should choice what to do. It's like there is no Button. I program on linux mint

Juan Gonzalez
Juan Gonzalez
11,848 Points

If anyone is still stuck, I encountered the same problem while running the app on a real device and emulator. After carefully inspecting the code i noticed this mistake i made:

mCurrentPage = mStory.getPage(0);

I simply forgot to change the zero to choice:

mCurrentPage = mStory.getPage(choice);

Hope this helps someone!