Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById() is returning unable to find symbol, but the ID is defined in the layout?

Tags:

java

android

I'm working on learning some Android app development by myself and I'm following a video tutorial on how to make a simple ToDo list application. However, I hit a compile error and from what limited knowledge I have, everything seems to be in order. Below is both the main class and the layout:

Main.java: http://pastebin.com/vfuANx0p

package com.example.exampletodo;

import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import android.widget.ArrayAdapter;

public class Main extends Activity implements OnClickListener, OnKeyListener {

    EditText txtItem;
    Button btnAdd;
    ListView listItems;

    ArrayList<String> toDoItems;
    ArrayAdapter<String> aa;

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

        txtItem = (EditText)findViewById(R.id.txtItem);
        btnAdd = (Button)findViewById(R.id.btnAdd);
        listItems = (ListView)findViewById(R.id.listItems);

        btnAdd.setOnClickListener(this);
        txtItem.setOnKeyListener(this);

        toDoItems = new ArrayList<String>();
        aa = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, toDoItems);
        listItems.setAdapter(aa);
    }

    private void addItem(String item){
        if(item.length() > 0){
            this.toDoItems.add(item);
            this.aa.notifyDataSetChanged();
            this.txtItem.setText("");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View v){
        if(v == this.btnAdd){
            this.addItem(this.txtItem.getText().toString());
        }
    }

    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
            this.addItem(this.txtItem.getText().toString());
        }
        return false;
    }

}

main.xml: http://pastebin.com/WcWg692v

<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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".Main">

    <EditText
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:id="@+id/txtItem" android:layout_alignParentTop="true"
           android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:focusable="true"/>
    <Button
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/add"
           android:id="@+id/btnAdd" android:layout_below="@+id/txtItem" android:layout_alignRight="@+id/txtItem"
           android:layout_alignLeft="@+id/txtItem"/>
    <ListView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/listItems"
           android:layout_alignParentRight="true" android:layout_alignParentLeft="true"
           android:layout_alignParentBottom="true" android:layout_below="@+id/btnAdd"/>
</RelativeLayout>

The error is:

 /home/rohan/ExampleToDo/ExampleToDo/src/main/java/com/example/exampletodo/Main.java
 Gradle: cannot find symbol variable main Gradle: cannot find symbol
 variable txtItem Gradle: cannot find symbol variable btnAdd Gradle:
 cannot find symbol variable listItems Gradle: cannot find symbol
 variable main

Thank you so much to anyone who is willing to help!

like image 554
rohan32 Avatar asked May 20 '13 22:05

rohan32


People also ask

How to Fix cannot find symbol error?

Output. In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

What does it mean when Java cannot find symbol?

A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means. A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

What does findViewById method do?

The findViewById() method is a method of Android's View and Activity classes. The method is used to find an existing view in your XML layout by its android:id attribute. The same can be done for any valid Android View object, such as a Button or a CheckBox view.

What is findViewById R ID?

findViewById is the method that finds the View by the ID it is given. So findViewById(R. id. myName) finds the View with name 'myName'.


1 Answers

You import wrong R file, you import android.R and you should import your package.R

com.example.exampletodo.R

EDIT: And don't forget clean and build is sometimes a must. So better do it your self than count on automatic R creation. And you can always check your R file whether it contains your ID-s.

part 2:

change this

ArrayAdapter<String>(this, R.layout.simple_list_item_1, toDoItems);

into

ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, toDoItems);

because simple_list_item_1 is part of android.R not your R

Hope this helps and enjoy your work.

like image 55
Marko Lazić Avatar answered Sep 26 '22 19:09

Marko Lazić