Kingsley
Kingsley

Reputation: 73

Why is ID not found?

I'm quite a newbie for the eclipse, and I've just started to write some code (actually it's just copy and paste from the website). Can anyone help me about this?

The error is appeared on the R.id.toggleButton1, I had added the line

android:id="@+id/toggleButton1"

in my layout activity_main.xml, but the error is still there :(

The error was:

toggleButton1 cannot be resolved or is not a field

The java source:

package com.example.ledgo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {


    ToggleButton tgbutton;
    private Activity activity;

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

        activity = this;

        tgbutton = (ToggleButton) findViewById(R.id.toggleButton1);
        tgbutton.setOnClickListener(new OnClickListener() {
        ...

The code for my layout xml or called activity_main:

<?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="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        ...

Upvotes: 1

Views: 8803

Answers (3)

Pararth
Pararth

Reputation: 8134

try
ctrl + shift + o

in Eclipse to organize imports, clean buil your app, that should work

Regarding the onClickListener(), it lets us tell the O.S what to do when that button is clicked.

onClick()

From View.OnClickListener. This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.

Read more about the Input Events

Here is a useful SO Link for onClickListener

Upvotes: 1

Joffrey
Joffrey

Reputation: 37660

Problem: Eclipse is telling you that toggleButton1 is not recognized as a field of R.id. This is probably because R.java was not generated properly.

Solution: To force eclipse to re-generate R.java, clean and rebuild your project. This can be done in Eclipse via Project > Clean...


Note: If R is not even recognized, it is probably because your activity is not in the main package, and you need to import R.java. This shouldn't be your case because you've got the error on the field level, not the R.

However, if ever it was the case, I would assume your base package is com.example. If so, you should add the following line below the other imports, just before your activity class declaration:

import com.example.R;

Upvotes: 5

Zohra Khan
Zohra Khan

Reputation: 5302

No need to import R.java.

In eclipse go to Project->Clean and run your application again.

Upvotes: 1

Related Questions