R.id.myEditText 错误 // todolist 尝试
嘿伙计们, 我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不必直接更新 R.java 文件,该文件是在构建项目时自动生成的。问题是您尚未将 EditText 添加到布局中:
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:
如果你改变
它
应该可以工作。
幕后发生了什么?当您在 XML 文件中定义具有特定 id 的元素时,Android 工具(在您的情况下可能是 Eclipse)将重新生成 R.java 文件。
R.java
永远不应该手动编辑,将其视为一个源文件,以类型安全的方式表示 XML 元素,您可以在 Java 代码中访问它。If you change
to
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.按如下方式更改您的 xml 文件,然后尝试:
Change your xml file as below and then try: