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
Kenan Abdulkarim
1,873 Points- When passing name to 2nd activity it shows as null - The 2 choice buttons in the StoryActivity are don't function
I am currently working through the build an interactive story app course and got stuck with the "loading additional pages" task/video. After following that video, you're supposed to be able to successfully tap a button that takes you to the next activity and from there anytime you tap on one of the choice buttons the app should load the appropriate page. Now my problem is that when I start the second activity (story activity) the choice buttons are invisible and have do not function at all. I've tried testing the app with different emulators as well as on a real android device and had no success. I also want to note that I tried copying the instructor's code directly from the download section and I still had the same problem.
Lastly, when I use the string.format() method to replace certain parts of the pageText string with a name that the user enters I get null instead of the actual name. In other words, when the user enters his/her name and taps "START YOUR ADVENTURE" button, the name entered by the user is replaces with null.
I'll post my code for the StoryActivity java file and its xml file.
package kenanabdulkarim.interactivestory.ui;
import android.content.Intent; import android.graphics.drawable.Drawable; 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 android.widget.Toast; import kenanabdulkarim.interactivestory.R; import kenanabdulkarim.interactivestory.model.Page; import kenanabdulkarim.interactivestory.model.Story; import kenanabdulkarim.interactivestory.model.Page; import android.app.Activity;
public class StoryActivity extends Activity {
// title option when creating a new activity only matters when we're running the action bar
// Convention for TAG
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story(); // need to load a page when 1) the activity is created
// 2) and when a button is tapped on
private ImageView mImageView;
private TextView mTextView;
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(); // returns the intent that started this activity
String mName = intent.getStringExtra(getString(R.string.key_name)); // pass in key we used
// key must be the same name we used to start this activity
if (mName.length() == 0) {
mName = getString(R.string.default_name);
}
Log.d(TAG, mName);
Toast.makeText(StoryActivity.this, "Hi " + mName, Toast.LENGTH_SHORT).show();
mImageView = (ImageView) findViewById(R.id.storyImageView);
mTextView = (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());
// getResources() --> We get the resources for the project through the activity
// getDrawable() --> And from those resources we are getting a drawable type
// pate.getImageId() --> then we are passing in this specific ID of the drawable that we want to get
mImageView.setImageDrawable(drawable);
// Once we have that drawable we set it as the image drawable for our ImageView
String pageText = mCurrentPage.getText();
// Add the name if placeholder included. Won't add if no placeholder (text already has appropriate characters needed)
// %1 = order number of the parameter (we only have 1 parameter so it's just going to be %1
// NOTE: If we wanted to replace more than 1 thing we could use different numbers instead of 1
// $s is just for a simple string
// pageText = String.format(pageText, mName);
pageText = String.format(pageText, mName);
mTextView.setText(pageText);
mChoice1.setText(mCurrentPage.getChoice1().getText());
// getChoice1() returns a choice object which we can use to get the text
mChoice2.setText(mCurrentPage.getChoice2().getText());
mChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// need to get index of new page we want to load
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);
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="#ffffffff" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="kenanabdulkarim.interactivestory.ui.StoryActivity">
<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"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stopAndInvestigate"
android:id="@+id/storyTextView"
android:layout_below="@+id/storyImageView"
android:layout_alignLeft="@+id/storyImageView"
android:layout_alignStart="@+id/storyImageView"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="15dp"
android:lineSpacingMultiplier="1.2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/choiceButton1"
android:id="@+id/choiceButton1"
android:background="#ffffffff"
android:textColor="#ff3a8aec"
android:layout_above="@+id/choiceButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<!-- android:style="@style/whiteButton" -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/choiceButton2"
android:id="@+id/choiceButton2"
android:background="#ffffffff"
android:textColor="#ff3a8aec"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/storyTextView"
android:layout_alignStart="@+id/storyTextView"/>
</RelativeLayout>
I've been stuck with these problems for a few days so I'd really appreciate the help.