2011-03-24

Corrections for Android Tutorial "Hello, Views > Spinner"

The Android Developer's guide (available at http://developer.android.com/guide/index.html) is an excellent resource for folks looking to develop Android applications. Of course, as is much technical documentation, it's infected with code bugs and errors.

In particular, this post lists errors in the "Hello, Views > Spinner" tutorial, available at http://developer.android.com/resources/tutorials/views/hello-spinner.html, and includes step-by-step corrections for these problems. This information was communicated to the Android development team in Android Issue 12817.
Link To This ArticleQR Code Link To This Articlehttp://goo.gl/P408h

Step 1: Start a new project named HelloSpinner.

The tutorial author(s)/editor(s) may be assuming the folks following this tutorial are already skilled enough to fill in the missing project creation details. For anyone stuck at this first step, take a look at the initial "Hello, World" tutorial. Following are reasonable values to use for the other fields when creating this new project.

Build Target:Android 1.5
Application name:Hello Spinner
Package name:fubar.hellospinner
Activity:HelloSpinner
Min SDK Version:3

Concerning step 2, it is (or will be) error free, after the third step is completed. It makes sense to me that steps two and three would be switched, so as to avoid the initial errors reported by Eclipse after completing the second step.

Step 3 introduces a significant error, because it instructs users to delete the app_name string resource value, which is actually used by the application. After following step 3 as instructed, save the changes, and notice that Eclipse complains about errors in AndroidManifest.xml concerning missing resource @string/app_name.

To correct this problem, instead of editing strings.xml to look like the given example, just add the new resource entries to the existing resource entries, so as not to delete the @string/app_name entry. In other words, following are reasonable contents for the strings.xml file.
<!--?xml version="1.0" encoding="utf-8"?-->
<resources>
<string name="hello">Hello World, HelloSpinner!</string>
<string name="app_name">Hello Spinner</string>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
Note the entry for "hello" was generated automatically when the project was created. It is not actually used by the application, and can be safely excluded.

For steps 4, 5, and 6, note the author(s)/editor(s) no doubt assumed readers would be able to provide the missing import statements, necessary for the code to compile. For these code changes, the following six items should be imported.
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
Step 5 contains a code error that needs to be corrected: it includes an extra closing parenthesis in the middle of the code. Correct this code
Toast.makeText(parent.getContext()), "The planet is " +
removing the extra ")" before the comma to then be
Toast.makeText(parent.getContext(), "The planet is " +
After implementing these corrections, users should be able to complete step 7, and successfully run the application.

Please don't hesitate to post any questions or problems you may still be having with the "Hello, Views > Spinner" tutorial below.

Happy Coding!

5 comments:

  1. main can not be resolved or is not a field (in setcontent view statement)
    spinner can not be resolved or is not a field(imeediate next statment of setcontent view)
    planets_array can not be resolved or is not a field(immediate next stmnt)



    These 3 errors i m getting.in strings.xml n in java file names r same.no mistkae there.
    stl gtng errorincluded all necessary packegs evn
    import android.R;
    this 1 also there ..still...
    plz help me..
    thnx in advance

    ReplyDelete
  2. I recommend posting help requests such as this to stackoverflow.com.

    If you're getting an error about not being able to resolve "main", I'd guess you're not setup correctly to build an Android project. The Hello, World Tutorial includes step-by-step instructions for getting started building Android projects.

    ReplyDelete
  3. Thanks for posting these updates. It was a big help debugging.

    One question on the application. When I run in the emulator before I select an item from the menu I get a Toast indicating the first item in the array has been selected. Is there a way I can have the listener not activate until I have actually selected an item.

    ReplyDelete
  4. Using this tutorial, I don't get the same problem you're reporting. At any rate, as already mentioned, I recommend posting help requests such as this to stackoverflow.com.

    ReplyDelete