R.id.myEditText 错误 // todolist 尝试

发布于 2024-10-29 11:12:22 字数 2494 浏览 1 评论 0原文

嘿伙计们, 我从 WROX(Android 2 应用程序开发)购买了一本 Android 开发书,在第 2 章中,他们有一个我似乎无法工作的平均任务列表项目。

我复制了确切的代码行并尝试调整它们以使其正常工作,但无论我做什么,以下行仍然是错误。

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

错误是R.id.myEditText,我已将其添加到 R.java 文件中,但不幸的是它不想工作。

它的java文件如下。


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;
        }
    });
}

}

XML 文件如下 有

<?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>

什么想法吗?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝咒 2024-11-05 11:12:22

您不必直接更新 R.java 文件,该文件是在构建项目时自动生成的。问题是您尚未将 EditText 添加到布局中:

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

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" 
    />
℉絮湮 2024-11-05 11:12:22

如果你改变

android:id="@+id/myTextView"

android:id="@+id/myEditText"

应该可以工作。

幕后发生了什么?当您在 XML 文件中定义具有特定 id 的元素时,Android 工具(在您的情况下可能是 Eclipse)将重新生成 R.java 文件。

R.java 永远不应该手动编辑,将其视为一个源文件,以类型安全的方式表示 XML 元素,您可以在 Java 代码中访问它。

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.

强辩 2024-11-05 11:12:22

按如下方式更改您的 xml 文件,然后尝试:

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文