0

I am learning Android development and I am using the book Professional Android 4 Application Development written by Reto Meier. I am using Eclipse with the ADT plugin to follow the examples in the book. I have already read several existing posts about this issue and have tried several methods to fix it. My code is exactly as the code is in the book. Please help me, I'm anxious to move on in the book but I don't want to skip past things but I've at this stupid bug for over three hours. It's a simple To-Do List app. Here's my main Activity class.

package com.example.todoolist;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoListActivity extends Activity {

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

    // Inflate your View

LINE 19 setContentView(R.layout.activity_to_do_list);

    // Get references to UI widgets

LINE 22 ListView myListView = (ListView)findViewById(R.id.myListView);

LINE 23 final EditText myEditText = (EditText)findViewById(R.id.myEditText);

    // Create the Array List of to do items
    final ArrayList<String> todoItems = new ArrayList<String>();

    // Create the Array Adapter to bind the array to the List View
    final ArrayAdapter<String> aa;

    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  todoItems);

    // Bind the Array Adapter to the List View
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new View.OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
        if (event.getAction() == KeyEvent.ACTION_DOWN)
          if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
              (keyCode == KeyEvent.KEYCODE_ENTER)) {
            todoItems.add(0, myEditText.getText().toString());
            aa.notifyDataSetChanged();
            myEditText.setText("");
            return true;
          }
        return false;
      }
    });

  }

}

Here's my XML file...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addItemHint"
    android:contentDescription="@string/addItemContentDescription"
  />
  <ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  />
</LinearLayout>

And my strings.xml...

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">ToDoList</string>
  <string name="addItemHint">New To Do Item</string>
  <string name="addItemContentDescription">New To Do Item</string>
</resources>

I went to my Android SDK Manager already and installed all the build tools that were not installed and then restarted the SDK Manager and cleaned my project several times and rebooted Eclipse and even rebooted my computer. Please help, at this point, I just want to move on and get this stupid error fixed. I've also added import statements and removed import statements as suggested by people on other posts. It did not fix the issue. Eclipse is giving the "R cannot be resolved to a variable" error three times in my Activity class (lines 19, 22, and 23).

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
JulianDavid
  • 123
  • 3
  • 14

1 Answers1

0

Same thing happen to me , and i passed a day to fix this. really this is a stupid error. i am just shearing my experience, may be it would helpful to you . First look carefully when you type this setContentView(R.layout.activity_to_do_list); hear your starting point R.l then eclipse should open a suggestion for you look like thisauto suggestion by eclipse

you import the second one not layout-android.R . the second one is created in your project hear com.example.gridtest is the project package name . then focus your imported section in your code import your R.java

ok look this import com.example.gridtest.R; its important . If you already import this android.R then remove it. Thank you hope it works . ( you don't need to do this always but if you face this kinda problem then do this )

Tanim reja
  • 2,120
  • 1
  • 16
  • 23