开发者

Android hellospinner tutorial

I am doing the HelloSpinner tutorial and I am getting error markers under two areas (lines 4 and 6 of the onCreate method...i marked them )and I cant figure out why? I have used the code from the tutorial and I have not varied from their instructions. Here is my code...

  package com.android.HelloSpinner;

   import android.app.Activity;

   import android.os.Bundle;
   import android.view.View;
   import android.widget.AdapterView;
   import android.widget.ArrayAdapter;
   import android.widget.Spinner;
   import android.widget.Toast;
   import android.widget.AdapterView.OnItemSelectedListener;

 public class Activity1 extends Activity {
       /** Called when the activity is fir开发者_高级运维st created. */
 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinner = (Spinner) findViewById(*R.id*(<-this is an error).spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, (*R.array*(<-this is an error).planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

}
public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
      Toast.makeText(parent.getContext(), "The planet is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}
   }////end of class Activity1

here is my main.xml file in layout

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:padding="10dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
     <TextView
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/planet_prompt"
/>
<Spinner 
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/planet_prompt"
  />
 </LinearLayout>

and her is my strings.xml file from my values folder

   <?xml version="1.0" encoding="utf-8"?>
  <resources>
  <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>


The R class is a generated class based on the all the resources in your res/ directory. The R class should be located in the 'gen' subdirectory of your project. So you can poke at it and see what it actually contains.

Here's more details on the R class: http://developer.android.com/guide/topics/resources/accessing-resources.html

See also: Android - How to regenerate R class?


None of the above worked for me. Somehow I ended up with spinner1 in my main.xml andI had to do change the java code to use this name.

Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);

This was a GUESS based on looking at some other api code from spinner1.java) but after looking at my main.xml it was obvious: Spinner android:id="@+id/spinner1"

The reason I ran into this is because I was playing with the elements of the main.xml manually, and added the spinner to the page before editing the xml. Hence, the system names it spinner1. Hope this helps someone avoid the headaches I ran into here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜