user3020345
user3020345

Reputation: 19

android app stopped but error free program

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.

Upvotes: 0

Views: 137

Answers (3)

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

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.

Upvotes: 1

Imran Iqbal
Imran Iqbal

Reputation: 173

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

Upvotes: 0

Nyx
Nyx

Reputation: 2243

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

Upvotes: 0

Related Questions