I'll p.s. this at the top:
I've transferred these files, as well as the files for the other activity and the manifest to a different project, changing only references to the project name and main activity and it runs just as it did before the weird behaviour. I guess this solves the problem, but I can't help thinking I've missed something somewhere. Why where those button instances remaining null?
Thanks,
Tony
========================== original post =========================================
I'm on Chapter 6 and I'm trying to use the ideas I've learned up to that chapter to create a front end for the contacts database. The perplexing thing is that the following code, which worked fine last time I tried it, has stopped working without any changes to this activity:
Code:
package uk.co.minminzemi.Database;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
private EditText nameBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameBox = (EditText) findViewById(R.id.nameText);
Button btnFind = (Button) findViewById(R.id.findBtn);
btnFind.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myFindHandler();
}
});
Button btnAdd = (Button) findViewById(R.id.addBtn);
btnAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myAddHandler();
}
});
}
private void myFindHandler() {
Toast.makeText(
getBaseContext(),
"You clicked Find with (" + nameBox.getText().toString()
+ ") in the box", Toast.LENGTH_SHORT).show();
}
private void myAddHandler() {
Intent i = new Intent("uk.co.minminzemi.AddContact");
Bundle extras = new Bundle();
extras.putString("Name", nameBox.getText().toString());
i.putExtras(extras);
startActivity(i);
}
}
The above code is acting on this main.xml and worked fine until now, when it throws a null pointer exception at (either of) the setOnClickListener calls, because the Buttons have the value null. I've rebuilt R.java, and the findBtn and addBtn values exist under the id section. What am I doing wrong? TIA for any pointers.
(A frustrated) Tony Martin
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter a contact name" />
<EditText
android:id="@+id/nameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/findBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Find Contacts" />
<Button
android:id="@+id/addBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Contact" />"
</LinearLayout>