0

Hi I am a beginner in android app development.I have been trying to develop 2 spinners following this link. http://examples.javacodegeeks.com/android/core/ui/spinner/android-spinner-drop-down-list-example/

I am getting an error R cannot be resolved to a variable. my MainActivity.java looks like this.

package com.example.androidspinnerexample;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

private Spinner spinner1, spinner2;
private Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}    // add items into spinner dynamically

      public void addItemsOnSpinner2() {
      spinner2 = (Spinner) findViewById(R.id.spinner2);
  List list = new ArrayList();
  list.add("Item 1");
  list.add("Item 2");
  list.add("Item 3");
  list.add("Item 4");
  ArrayAdapter dataAdapter = new    ArrayAdapter(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
   }

    public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
  }

    public void addListenerOnButton() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
button = (Button) findViewById(R.id.button);

button.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    Toast.makeText(MainActivity.this,"Result : " +   "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +  "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),Toast.LENGTH_SHORT).show();
  }

});
    }
    }

In my console i see this error as well. My Projects\AndroidSpinnerExample\res\menu\main.xml:6: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_settings').

my main.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/country_array"
    android:prompt="@string/select"/>

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/select2" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_label" />
</LinearLayout>

My R.java file is also missing. I tried googling all the solutions that wer posted in stack overflow so far but did not work. Can anyone plz take a look into it ? .

My strings.xml looks like this.

<?xml version="1.0" encoding="utf-8"?>

 <resources>
<string name="app_name">AndroidSpinner</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="select">Choose a country</string>
<string name="select2">Choose an item</string>
<string name="button_label">Submit</string>
<string-array name="country_array">
    <item>Greece</item>
    <item>United Kingdom</item>
    <item>Italy</item>
    <item>France</item>
    <item>Germany</item>
    <item>Turkey</item>
    <item>Poland</item>
    <item>India</item>
</string-array>
</resources>

I could resolve my R variable issue but now my app doesnt open and it says that the app stopped.But my program is error free.

user3020345
  • 19
  • 2
  • 9

3 Answers3

1

Try this :

Press Ctrl + Shift + Letter "O" if you are developing your application in Eclipse. It will import the files for you.

Also, Add this string in "strings.xml" file :

<string name="action_settings">Your String name</string>

Hope this helps.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0

The R.java file cannot be created if there is an error in any file in your /res folder.

Nyx
  • 2,233
  • 1
  • 12
  • 25
  • yes. But i could not see anything wrong in my res folder. error is not shown in my xml files but in the console it shows No resource found that matches the given name (at 'title' with value '@string/action_settings'). – user3020345 Mar 13 '14 at 21:58
  • run Lint. You may find the answer that way. – Kristy Welsh Mar 13 '14 at 22:25
  • It sounds like you're using the string action_settings in a file in your res/menu folder but it isn't defined in strings.xml To fix this, either define action_settings in strings.xml, or go to res\menu\main.xml and remove/change the the line that references action_settings – Nyx Mar 13 '14 at 23:52
  • thanks nyx.. I could resolve that.but now my issue is my app gets stopped in the avd. – user3020345 Mar 16 '14 at 18:21
  • Can you post the error that comes up in LogCat when the app stops? – Nyx Mar 17 '14 at 01:16
0

You told error is at AndroidSpinnerExample\res\menu\main.xml but your question do not have that code . The code which you pasted is from layouts please paste code from menu folder main.xml file

Imran Iqbal
  • 163
  • 1
  • 11