I’m trying to implement an Android spinner in my app but am running into some problems with the java file associated with the Activity its placed in.
The errors I am getting are as follows:
1. setContentView(R.layout.activity_display_asleep_at);
- cannot be resolved or is not a field
2. cycles_array (where ArrayAdapter<CharSequence> is...)
- cannot be resolved or is not a field
3. spinner (where "android.widget.Spinner spinner" is...)
- cannot be resolved or is not a field
The code I have is as follows:
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.Spinner;
public class DisplayAsleepAt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_asleep_at);
getActionBar().setDisplayHomeAsUpEnabled(true);
android.widget.Spinner spinner = (android.widget.Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.cycles_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Here is the xml file I am using:
<Spinner
android:id="@+id/AA_spinner_prompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/AA_spinner_promptTXT"
android:layout_alignParentRight="true" />
And here is the strings.xml file I have the array in:
<string-array name="cycles_array">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
If anyone has any ideas as to what might have gone wrong, it would be greatly appreciated. I can always post more if you need. Thanks all.
EDIT:
Here is the entire xml file I am using. I’ve been looking through it for a while and can’t seem to find anything wrong with it…maybe I’m just tired at this point…
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp" >
<TextView
android:id="@+id/AA_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AA_titleTXT"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:textSize="35dp"
android:textStyle="italic"
tools:context=".DisplayAsleepAt" />
<TextView
android:id="@+id/AA_intro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AA_introTXT"
android:textSize="15dp"
android:layout_below="@id/AA_title" />
<Button
android:id="@+id/AA_button_pick"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/AA_button_pickTXT"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:onClick="showTimePickerFragment" />
<TextView
android:id="@+id/AA_rem_cycles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AA_rem_cyclesTXT"
android:textSize="15dp"
android:layout_marginBottom="20dp"
android:layout_below="@id/AA_button_pick" />
<Spinner
android:id="@+id/AA_spinner_prompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/AA_spinner_promptTXT" />
<TextView
android:id="@+id/AA_final_time"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="@string/AA_final_timeTXT"
android:textSize="15dp" />
<Button
android:id="@+id/AA_create_alarm_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/AA_create_alarm_buttonTXT"
android:layout_centerHorizontal="true" />
</RelativeLayout>
The error means there’s something syntactically wrong with your XML layouts and R cannot be auto-generated. Try looking at your xml code and if you still have problem then post your xml code.
Change this code
To
Hope it helps