0

I'm following through the first TodoList example in Reto Meier's wonderful Professional Android 4 Application Development, but having a slight problem I haven't been able to work out. Here's the code as it is:

package com.paad.todolist;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoListActivity extends Activity {

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

        // Get references to UI widgets
        ListView myListView = (ListView) findViewById(R.id.myListView);
        final EditText myEditText = (EditText) findViewById(R.id.myEditText);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_to_do_list, menu);
        return true;
    }
}

This differs slightly from the book, which uses main.xml. The later Eclipse creates activity_to_do_list.xml, which has the same content as the book.

My original problem was that R.id.myListView and R.id.myEditView weren't being recognised, and I tried Project | Clean to solve this. This made things worse, now "R cannot be resolved to a variable". I suspect the clean removed generated code.

Also I have an error in the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.paad.todolist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ToDoListActivity"
            android:label="@string/title_activity_to_do_list" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

@string/title_activity_to_do_list doesn't work, not too surprising as the clean seems to have removed res\values\string.xml too.

Help!

Hamish McNeill
  • 397
  • 2
  • 8

2 Answers2

0

You need to create the string.xml file. In it you need to define your string for title_activity_to_do list. Your manifest file picks up the value from string.xml. Like this:

<resources>

    <string name="app_name">APP NAME</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_to_do">MY NAME</string>

</resources>

When your referencing errors have been fixed, Clean project again and your "R cannot be resolved to a variable" error should be resolved.

CocoNess
  • 4,213
  • 4
  • 26
  • 43
  • Thank you - I could have sworn that I created strings.xml and the project clean deleted it, but all the evidence is against me! Working now. – Hamish McNeill Nov 20 '12 at 19:57
0

You have to create res/values/strings.xml and inside put those strings with this format:

If a layout, a res, is not correct, then R is not generated properly (or not generated) and that's why other things begin to show an error.

Jon Zangitu
  • 957
  • 1
  • 15
  • 30