p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 (http://p2p.wrox.com/forumdisplay.php?f=432)
-   -   Chaoter 2 To Do List: R.layout.main cannot be resolved (http://p2p.wrox.com/showthread.php?t=74574)

ik09 May 25th, 2009 05:57 PM

Chaoter 2 To Do List: R.layout.main cannot be resolved
 
Hello! I am just getting started with Eclipse and Android development, so I apologize if this is a simple fix. After working through the To Do List example, I am unable to run my application in the emulator because of three errors in ToDoList.java. I also have numerous errors in the R.java auto-generated file. I am running the current version of Eclipse, with Android 1.5r SDK and the current version of Java. I will post all of my code, and if anyone could read over it and let me know where my error is, I would greatly appreciate it!

src/org.example.TodoList/TodoList.java:

Code:

package org.example.TodoList;

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

public class ToDoList extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // Inflate your view
        setContentView(R.layout.main); [:confused:]  Eclipse error: R.layout.main cannot be resolved

        // Get references to UI widgets
        ListView myListView = (ListView)findViewById(R.id.myListView);  [:confused:] Error: R.id.myListView cannot be resolved
        final EditText myEditText = (EditText)findViewById(R.id.myEditText); [:(!]  Error: R.id.myEditText cannot be resolved

       
        //Create the array of to-do list items
        final ArrayList<String> todoItems = new ArrayList<String>();
        //Create the array adapter to bind the array to the listview
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
                                        android.R.layout.simple_list_item_1,
                                        todoItems);
        //Bind the array adapter to the listView
        myListView.setAdapter(aa);
       
    // Add key listener to add the new todo item
        // when the middle D-pad button is pressed.
        myEditText.setOnKeyListener(new OnKeyListener() {
          public boolean onKey(View v, int keyCode, KeyEvent event) {
          if (event.getAction() == KeyEvent.ACTION_DOWN)
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
              // Add the new todo item, and clear the input text box
              todoItems.add(0, myEditText.getText().toString());
              myEditText.setText("");
              aa.notifyDataSetChanged();
              return true;
            }
          return false;
        }
      });
       
    }
   
  }

res/layout/main.xml:
Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New To Do Item"
        />
        <ListView
            android:id="@+=id/myListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</LinearLayout>



res/values/strings.xml:
Code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">To Do List</string>
    <string name="add_new">Add New Item</string>
      <string name="remove">Remove Item</string>
</resources>

Many thanks in advance! Thank you for the work you have done on this book Reto, it has been a joy to read so far!

sierawsk May 26th, 2009 10:32 AM

You have a typo in the XML definition of myListView. It should be:

Code:

android:id="@+id/myListView"
(get rid of the '=').

Jeff S.

ik09 May 26th, 2009 11:00 AM

Many thanks Jeff. Correcting that typo cleared my errors in R.java. I still had errors in TodoList.java however. Removing
Code:

import android.R
fixed that issue and now I am up and running.

vivekanand3435 December 8th, 2009 08:26 AM

I am also facing the same problem, and ur correction did not work for me. Can someone help me fix this problem.
Thanks in advance!!!!


All times are GMT -4. The time now is 02:13 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.