Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App Finishing the User Interface Loading the First Page

cannot find symbol

hi I want bulis ABK . but after i pres build the have some mistake :

method getShortExtra in class Intent cannot be applied to given types; required: String,short found: String reason: actual and formal argument lists differ in length

cannot find symbol class intent

cannot find symbol variable name

all of that in StoryActivity java

the file :

package com.example.interactivitystory.ui;

import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat;

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 com.example.interactivitystory.R; import com.example.interactivitystory.model.Page; import com.example.interactivitystory.model.Story;

import java.util.Stack;

public class StoryActivity extends AppCompatActivity {

public static final String TAG = StoryActivity.class.getSimpleName();
private String name;
private Story story ;
private ImageView storyImageView;
private TextView storyTextView;
private Button choice1Button;
private Button choice2Button;
private Stack<Integer> pageStack= new Stack<Integer>();




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

    storyImageView =(ImageView)findViewById(R.id.storyImageView) ;
    storyTextView = (TextView)findViewById(R.id.storyTextView);
    choice1Button = (Button)findViewById(R.id.choice1Button);
    choice2Button = (Button)findViewById(R.id.choice2Button);


    Intent intent = getIntent();
     name = intent.getShortExtra( "name");
   // String name = intent.getShortExtra( R.string.key_name);
    if (name== null || name.isEmpty()) {
        name = "Friend";
    }
    Log.d(TAG, name);

    story = new Story();
    loadPage(0);



}

private void loadPage(int pageNumber) {
    pageStack.push(pageNumber);

    final Page page = story.getPage(pageNumber);

//ضفت فاينل Drawable image = ContextCompat.getDrawable(this, page.getImageId()); storyImageView.setImageDrawable(image);

    String pageText= getString(page.getTextId());
    // Add name if placeholder included. Won't add if not
    pageText = String.format(pageText, name);

    storyTextView.setText(pageText);

    if (page.isFinalPage()){
        choice1Button.setVisibility(View.INVISIBLE);
        choice2Button.setText(R.string.play_again_botton_text);
        choice2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadPage(0);
            }
        });

    }

    else {

        loadButtons(page);

    }

    // هنا نسخت التشويس ٢

}

private void loadButtons(final Page page) {
    choice1Button.setVisibility(View.VISIBLE);
    choice1Button.setText(page.getChoice1().getTextId());
    choice1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = page.getChoice1().getNextPage();
            loadPage(nextPage);
        }
    });

    choice2Button.setVisibility(View.VISIBLE);
    choice2Button.setText(page.getChoice2().getTextId());
    choice2Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = page.getChoice2().getNextPage();
            loadPage(nextPage);
        }
    });
}

@Override
public void onBackPressed() {
    pageStack.pop();
    if (pageStack.isEmpty())
    {
        super.onBackPressed();
    }
    else {
        loadPage(pageStack.pop());
    }
}

}

sorry for the comment in Arabic .. : )

3 Answers

Steven Parker
Steven Parker
218,812 Points

The first message relates to this part of the code:

     name = intent.getShortExtra( "name");

When it says "required: String,short" it means that the method "getShortExtra" is defined to take two arguments, one String and one short. Then "found: String" is pointing out that where it is used in the code, only a String argument is given. And finally the message "reason: actual and formal argument lists differ in length" is just reconfirming that the method needs two arguments but it was only given one.

If you need additional help, please use Markdown formatting for future questions. Or better yet, make a snapshot of your workspace and post the link to it here. disregard that, I overlooked that this project is not done in a workspace

Thank you for helping .. but how can I fix the problem .. ?

I try to do snapshot .. but I can't from Android studio ..

Steven Parker
Steven Parker
218,812 Points

To fix that particular issue, you must supply a 2nd argument (the default value).

how about the Error : cannot find symbol class intent ?

Steven Parker
Steven Parker
218,812 Points

The workspace isn't helpful since it has no support packages installed, and the project is intended for Android Studio anyway, I previously suggested it out of habit. :see_no_evil:

Perhaps someone currently working on the same project can provide input on the other errors.

But do fix the formatting in the posted code above!