因此,从Android和Java开始,我有这个莫名其妙的错误:当达到findViewByID(R.id.answer)时,应用程序失败并且意外停止消息,但仅当我添加另一个一个代码。Android应用程序上的findViewById失败
的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/site" />
<EditText android:id="@+id/site"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<!--
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username" />
<EditText android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
-->
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" />
<EditText android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question" />
<EditText android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer" />
<EditText android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<Button android:id="@+id/confirm"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.keyedit);
setTitle(R.string.edit_entry);
mDbHelper = new KeyRingDbAdapter(this);
mDbHelper.open();
mSiteText = (EditText) findViewById(R.id.site);
//mUsernameText = (EditText) findViewById(R.id.username);
mPasswordText = (EditText) findViewById(R.id.password);
mQuestionText = (EditText) findViewById(R.id.question);
mAnswerText = (EditText) findViewById(R.id.answer);
所以,上述工程和线mAnswerText = (EditText) findViewById(R.id.answer);
分配权对象mAnswerText
。一旦我取消注释代码,之前工作过的同一行将失败。当我在图形布局上看到xml时,所有内容都显示正确。 R文件似乎没问题(为了以防万一,我删除了它并重新生成)。
任何想法?需要更多信息?
编辑:好吧,尴尬。昨天晚上我回到家,当我没有注意到这些线条时,这一次它就起作用了。作为程序员,我有足够的经验,我做了足够的检查,知道不是我只是取消注释部分,或者做一些完全愚蠢的事情。虽然它可能与我对Eclipse新手也是如此。不是很酷,因为现在这个错误已经被“神奇地”修复了,而且我并不聪明。
您已经评论了java代码和xml文件中的元素 - 所以,只要确定,如果您取消注释*两个*部分(不仅仅是java代码中的行),应用程序就会失败! – 2011-05-26 14:41:05
请发布完整的logcat错误消息。 – Haphazard 2011-05-26 14:45:19
@Andreas_D是的,取消注释。 – 2011-05-26 14:59:38