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) Intents and Multiple Activities Adding a Second Activity

When i run the app and click on the START YOUR ADVENTURE button the app crashes. I don't know how to fix it.

The logcat says, "Unable to find explicit activity class {com.example.jacob.interactivestory/com.example.jacob.interactivestory.StoryActivity}; have you declared this activity in your AndroidManifest.xml?" How do i go about fixing this problem?

package com.example.jacob.interactivestory;

import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends Activity {

private EditText mNameField;
private Button mStartButton;

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

    mNameField = (EditText) findViewById(R.id.nameEditText);
    mStartButton = (Button) findViewById(R.id.startButton);

    mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = mNameField.getText().toString();
            startStory();
        }
    });
}

private void startStory() {
    Intent intent = new Intent(this, StoryActivity.class);
    startActivity(intent);
}

}

"The R in R.layout.activity_main is an error as with all the other R's throughout the both activities" Not sure if this has to do with my first Question but i think it might help lead you to a answer my first question. If the R error is the problem how should I fix it? Thanks

2 Answers

L B
L B
28,323 Points

Go into your manifest file and the following code:

<activity
      android:name=".StoryActivity"
      android:label="Story"></activity>

Thanks, it worked, I was getting frustrated with all the problems I have faced, and trying to figure out this one was a headache being a beginner at this stuff. Thanks again

I am having a similar problem, but it's not "crashing" it just doesn't do anything when I tap the button. In your answer LB, you say to add that code in the manifest file. Do you mean the manifest file for the StoryActivity? If so, is it the first or 2nd activity?

L B
L B
28,323 Points

@Terence Evans The manifest file is its own file. You should see it at the top of the file structure if you are using Android Studio.

OK, I see. I tried the suggestion above but it didn't work. I got lots more errors. This is what my AndroidManifest.xml currently looks like: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.twesq.interactivestory" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".StoryActivity"
        android:label="@string/title_activity_story"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.twesq.interactivestory.MainActivity" />
    </activity>

</application>

</manifest>