开发者

R.id.myEditText error // todolist attempt

Hey guys, i bought an android dev book from WROX (Android 2 application development) and on chapter 2 they have an average task list project that i can't seem to get开发者_StackOverflow working.

I copied the exact line of code and tried tweaking them to get it to work but the following line remains an error no matter what i do.

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

the error is the R.id.myEditText, i've added it to the R.java file but no luck it doesn't want to work.

the java file for it is the following.


package com.paad.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; 

public class ToDoList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView myListView = (ListView) findViewById(R.id.myListView);
    final EditText myEditText = (EditText) findViewById(R.id.myEditText);

    final ArrayList<String> todoItems = new ArrayList<String>();

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

    myListView.setAdapter(aa);

    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) {
                    todoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }
            return false;
        }
    });
}

}

the XML file is the follwing

<?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"
    >
<TextView
    android:id="@+id/myTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    />
</LinearLayout>

Any ideas?


You dont update your R.java file directly, this is generated automatically when your project is built. The issue is that you have not added the EditText to your layout:

<EditText
    android:id="@+id/myEditText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />


If you change

android:id="@+id/myTextView"

to

android:id="@+id/myEditText"

it should work.

What's happening under the hood? When you define an element with a specific id in XML file, Android tools (Eclipse probably in your case) will regenerate R.java file.

R.java should never be edited by hand, think of it as a source file that represents your XML elements in a type safe way which you can access in Java code.


Change your xml file as below and then try:

<?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="@string/hello"     /> <ListView     android:id="@+id/myListView"     android:layout_width="fill_parent"     android:layout_height="wrap_content"      /> </LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜