1

I am posting this as a new question, although I have managed to find the solution to the problem myself, and despite the fact that the question has been asked before, either directly or indirectly. I mention the following posts at StackOverflow as examples:

stackoverflow.com/questions/39872779/extra-message-and-putextra

stackoverflow.com/questions/38938297/android-studio-cannot-find-symbol-variable

The reason for doing this post, is that the issue seems to be a recurring pitfall among beginners (myself included) who follow the guide "Building Your First App" by Android Developers. It seems difficult to find the answer explaining specifically why the problem arises. I spent several hours plundring in Android Studio and searching online, until I found the answer.

Description of the problem:

While having followed the step "Start Another Activity", thus attempting to run the application, you may experience Build error, with the following message:

"error: cannot find symbol variable EXTRA_MESSAGE"

In addition, instances of EXTRA_MESSAGE are colored red in the programming code in Android Studio.

It can be hard to understand why this error occurs when you have followed the description in the guide to the best of ability. A brief answer that have been given a number of times is that you have not declared the EXTRA_MESSAGE variable as public static in the MainActivity. But this provides no explanation of how the declaration should have been performed (syntax), or how you have managed to avoid the declaration in the first place.

SeagulFish
  • 113
  • 1
  • 2
  • 12
  • @Ken White Very well! Thank you for your information. :-) Sorry about that mistake. I have edited the question, and added an answer instead. – SeagulFish Mar 30 '17 at 22:57

1 Answers1

1

A long answer:

In MainActivity.java, the following phrase is missing:

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

The phrase declares a symbol variable called EXTRA_MESSAGE, of string-type. The symbol variable is public constant, hence it’s available in all methods in the application, not only MainActivity. The phrase actually appears in the guide, but it is easy to overlook, as a result of the way the guide is designed:

At step 1 under "Respond to the send button", you are prompted to add the method sendMessage(), shown as follows:

(Screenshot) Respond to the send button

Currently, the method performs nothing, as only a comment is inserted where the program code should be. Later, under "Build an Intent", you are prompted to add the EXTRA_MESSAGE constant and sendMessage() code, shown as follows:

(Screenshot) Build an Intent

At this point, you’ll mess up if you simply copy the program code under the method sendMessage(), while ignoring that you also need to add the new phrase above the sentence "@Override."

However, the guide provides poor explanation of the terms of the sentence: "Public", "final", "static" and "String", and the difference between EXTRA_MESSAGE and com.example.myfirstapp.MESSAGE. This could have been improved, I think.

I hope this post will be of help to other beginners who are stuck with the same issue.

SeagulFish
  • 113
  • 1
  • 2
  • 12
  • I also work through the training and had no problems so far. But I do not understand one thing, which is not explained (or I do not find it): Why is the String value of the global constant exactly´com.example.myfirstapp.MESSAGE´? Is it just a symbolic placeholder and could also be "blablabla" oder does it have to be exactly this value and if so what does it do/say? Does is refer to a Message class? – ho.s Jul 10 '17 at 09:22
  • 1
    @ho.s From what I read in [this thread](https://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data), the String value could be anything, like "Foo", "bar" or "HoRsE". You don't really need to declare the string at all, if you write the code differently. In MainActivity.java you could write `intent.putExtra("blablabla", message);`, if you write `String message = intent.getStringExtra("blablabla");` in DisplayMessageActivity.java. You only need to refer to the exact same string value in both activities to make it work. The guide only shows one way to do this. – SeagulFish Jul 11 '17 at 20:31
  • thank you, I thought maybe I oversee sth. and the *.MESSAGE* part is totally meaningful. Good explanation and helpful link to the other thread. – ho.s Jul 12 '17 at 06:32