user3037567
user3037567

Reputation: 3

id cannot be resolved or is not a field, Button listener

I'm pretty sure i'm missing something really easy here... I'm new to android so i'm sorry about this. I think i'm not linking the buttons to the listener properly, basicly what I think i'm doing is making a listner (for buttons) and checking which button has been pushed,

MainMenu.java

package com.coursework;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class mainMenu extends Activity{

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mainmenu);

    TextView txt = (TextView) findViewById(R.id.gameName);
    Typeface font = Typeface.createFromAsset(getAssets(), "RetroElectro.ttf");
    txt.setTypeface(font);


    Button.OnClickListener listener = new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.id == R.id.play){
                Log.w("Test","one");
            }
else if(v.id == R.id.options){
                Log.w("Test","one");
            }

        }

};
((Button)findViewById(R.id.play)).setOnClickListener(listener);

} }

XML

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

      <TextView 
    android:id="@+id/gameName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="@string/gameName"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:layout_marginTop="20dp"
    android:textSize="80sp"
    />

  <Button
    android:layout_width="@dimen/button"
    android:id="@+id/play"
    android:layout_height="wrap_content"       
    android:layout_centerHorizontal="true"
    android:layout_marginTop="120dp"
    android:text="@string/play"></Button>




  <Button
    android:layout_width="@dimen/button"
    android:id="@+id/options"
    android:layout_height="wrap_content"       
    android:layout_centerHorizontal="true"
    android:layout_below="@id/play"
    android:text="@string/options"></Button>
   <Button
    android:layout_width="@dimen/button"
    android:id="@+id/scoreboard"
    android:layout_height="wrap_content"       
    android:layout_centerHorizontal="true"
    android:layout_below="@id/options"
    android:text="@string/score"></Button>
    <Button
    android:layout_width="@dimen/button"
    android:id="@+id/exit"
    android:layout_height="wrap_content"       
    android:layout_centerHorizontal="true"
    android:layout_below="@id/scoreboard"
    android:text="@string/exit"></Button>

</RelativeLayout>

The part which is the problem is the line if(v.id == R.id.play){ and if(v.id == R.id.play){ with an error,

id cannot be resolved or is not a field

Upvotes: 0

Views: 309

Answers (3)

Joel Fernandes
Joel Fernandes

Reputation: 4856

It's not v.id

Call the method getId() It will return the view's identifier.

Ex:

v.getId()

That is:

if(v.getId() == R.id.play){ 

//statements 

}

http://developer.android.com/reference/android/view/View.html#getId()

Upvotes: 2

FinalDark
FinalDark

Reputation: 1748

You should use .getId() instead of .id

Like this:

v.getId();

Upvotes: 0

Buddy
Buddy

Reputation: 11028

Try importing your app's R file, like:

import com.coursework.R;

Upvotes: -1

Related Questions