So, starting with Android and Java, and I have this baffling error: The application fails with a stopped unexpectedly message when reaches a findViewByID(R.id.answer) but only if I add another one to the code.
The code is as below:
<?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 method
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);
So, the above works and the line mAnswerText = (EditText) findViewById(R.id.answer); assigns the right object to mAnswerText. Once I uncomment the code, that same line, that has worked before, fails. When I see the xml on the graphical layout, all appears correctly. The R file seems to be ok (I deleted it and go it regenerated, just in case).
Any ideas? Any more info needed?
EDIT: Well, awkward. Last night I got home and, when I uncommented the lines, this time it worked. I am experienced enough as programmer and I did enough checking to know that it wasn’t me just uncommenting part or doing something else entirely stupid. Though it could have been something related to the fact that I am new to Eclipse as well. Not cool, as right now the error has been fixed “magically” and I am not the wiser.
I faced the same issue today. In a RelativeLayout I reordered the layout xml file – and got this error.
I cleaned the project (Eclipse: Project / Clean) et voilà it worked again.
Eclipse help: “Clean – This command discards all previous build results. If autobuild is on, then this invokes a full build.”