Android Development Tutorial: Starting Another Activity has Errors

New to Android and java development I have started in the tutorials on http://developer.android.com/training/ but not without major issues. While the first 3 lessons went Ok, the fourth, “Starting Another Activity” simply would not run properly. Yes, it could compile without error, but when run it would abort, all without an error message. Here is where I found the solution, thanks to Hariharan.

Learning Android, Java and Eclipse is not so easy for the beginner. Even though I have about 15 different languages under my belt, Android is complex. I was thankful that developer.android.com had a set of tutorials. Still, it is difficult for a beginner to debug something as complex as an Android app when they are just starting out. Damn, developer.android.com, you really need to get your act together because so many new developers like myself rely on these tutorials, and if they do not function properly we have nowhere to go, other than an internet search. Why not fix the problem in your tutorial?

After checking my code twice against the tutorial, the app would run, but when I hit it would say that there was an error and Android could not continue. I would hit a button and the app would close. I was getting this really odd error message in Eclipse:


java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button

Here are the documented changes, for those of us newbies that need a bit more help.

MainActivity.java: The bulk of the changes are here.
The first code block from Hariharan goes here. It includes a new field editText

In MainActivity.java the Placeholder Fragment function is changed by Hariharan by the addition of the editText field, editText statement, 2 Button statements as well the additional onClick function. Remove the extra quote in the first Button statement in Hariharan’s solution, as it is a syntax error.

Hit O to include the android.view.View.OnClickListener library, which has the setOnClickListener function.

fragment_main.xml: the Button declaration, add an android:id. Remove the original android:onClick.

DisplayMessageActivity.java: -add a new string EXTRA_MESSAGE

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

The original tutorial has a code block titled “The DisplayMessageActivity class should now look like this:”, but this is not the code for DisplayMessageActivity.java but for MainActivity.java. The tutorial then confuses readers further down in the “Display the Message” section, under “The complete onCreate() method for DisplayMessageActivity now looks like this:”. This is the correct codeblock for DisplayMessageActivity. This error really confused me and as a newby I would not have been able to fix this myself. This is terrible for a tutorial. I am actually dreading continuing with the tutorial series because of possible future blatant errors that may occur in the tutorial.

After a save and compile the app runs as it should: input into a field, submit, and the input is displayed on a second screen. The app is so simple yet greatly confused by sloppiness of the tutorial writers.

Hariharan’s solution works but I and many other newbies cannot tell others that it works because StackOverflow requires us to have over 50 reputation before we can add a comment. This is not good for a newby.

Note: The stack overflow solution thread http://stackoverflow.com/questions/23298045/android-my-first-app-tutorial-androidonclick-issues does not work. Discard this advice.

Leave a Reply

Your email address will not be published. Required fields are marked *