2011-05-26 48 views
0

因此,从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新手也是如此。不是很酷,因为现在这个错误已经被“神奇地”修复了,而且我并不聪明。

+0

您已经评论了java代码和xml文件中的元素 - 所以,只要确定,如果您取消注释*两个*部分(不仅仅是java代码中的行),应用程序就会失败! – 2011-05-26 14:41:05

+0

请发布完整的logcat错误消息。 – Haphazard 2011-05-26 14:45:19

+0

@Andreas_D是的,取消注释。 – 2011-05-26 14:59:38

回答

0

我今天面临同样的问题。在RelativeLayout中,我重新排列了布局xml文件 - 并得到了这个错误。

我清理了项目(Eclipse:Project/Clean)等等,它再次运行。

Eclipse帮助:“清洁 - 该命令放弃所有以前的构建结果,如果autobuild打开,则调用完整构建。

0

确定您导入了正确的R?有两个,确保你没有输入错误的...

+0

我会检查。但是,如果我导入了错误的,那么注释行就会失败,因为它具有新的ID,而不是ID已经工作的那一行。情况不是这样吗? – 2011-05-26 15:11:36

+0

是的,你是对的......但是,这发生在我身上的一个观点,这就是为什么我想提及它;) – 2011-05-27 05:42:51

+0

我很确定我会在未来找到这样的问题,所以我会保持这个金块信息:-) – 2011-05-27 08:30:47

相关问题