Wrox Programmer Forums
|
BOOK: Beginning Android Application Development
This is the forum to discuss the Wrox book Beginning Android Application Development by Wei-Meng Lee; ISBN: 978-1-1180-1711-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2011, 01:53 PM
Registered User
 
Join Date: Nov 2011
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default Button reference null - sanity check

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>

Last edited by mudskipper; November 29th, 2011 at 10:12 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
null reference object error GenXisT BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 3 January 15th, 2010 11:42 AM
Null reference issue Abbas C# 1 March 19th, 2007 11:39 PM
Null Reference Exception was unhandled labby C# 2005 1 February 9th, 2007 03:15 PM
Null reference exception when using DataAdapter Samantha Karen Webb C# 1 July 21st, 2006 09:12 AM
NULL reference exception Dwizz VB.NET 2002/2003 Basics 3 June 21st, 2005 07:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.