Android Forums

Go Back   Android Forums > Android Coders > Android Development
Connect with Facebook

Click Here To Register!
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-08-2010, 04:14 AM   #1
Junior Member
 
Join Date: Feb 2010
Posts: 1
Friends: 0
View pocnib's Profile   View pocnib's Photo Album   View pocnib's Blog   View Social Groups
*SOLVED*

Solved, I just needed to setContentView before getting the Button from the xml file



I am new to android development, experienced with Java. I am trying, basically, to play around with OnClick events, using a button click to change Layouts. I started by making two .xml files in my Layouts folder of the project and just setting setContentView to the first xml in onCreate and to the other xml in onClick. Through some searching I found that another Activity and an Intent object. I tried this with the following:

Code:
package org.Android.HelloAndroid;

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;

public class NextActivity extends Activity{
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		Button button = (Button) findViewById(R.id.goback);
		button.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				Intent myIntent = new Intent(v.getContext(), Hello_Android.class);
				startActivityForResult(myIntent,0);
			}
		});
		setContentView(R.layout.onclick);
	}
}
Code:
package org.Android.HelloAndroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class Hello_Android extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle created) {
        super.onCreate(created);
        TextView tv = (TextView) findViewById(R.id.textview);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		   Intent myIntent = new Intent(v.getContext(),NextActivity.class);
        		   startActivityForResult(myIntent,0);
        	   }
        });
        this.setContentView(R.layout.main);
    }
}
When I run this I get the error:

"The application Hello_Android (process org.android.HelloAndroid) has stopped unexpectedly. Please try again. Force Close."

I used logcat, which I will be the first to admit I am unfamiliar with how to read, but what stood out was the java Stack trace,

Code:
E/AndroidRuntime(  180): Uncaught handler: thread main exiting due to uncaught exception

E/AndroidRuntime(  180): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.Android.HelloAndroid/org.Android.HelloAndroid.Hello_Android}: java.lang.NullPointerException

E/AndroidRuntime(  180): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

E/AndroidRuntime(  180): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

E/AndroidRuntime(  180): 	at android.app.ActivityThread.access$2200(ActivityThread.java:119)

E/AndroidRuntime(  180): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

E/AndroidRuntime(  180): 	at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(  180): 	at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(  180): 	at android.app.ActivityThread.main(ActivityThread.java:4363)

E/AndroidRuntime(  180): 	at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(  180): 	at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(  180): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

E/AndroidRuntime(  180): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

E/AndroidRuntime(  180): 	at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  180): Caused by: java.lang.NullPointerException

E/AndroidRuntime(  180): 	at org.Android.HelloAndroid.Hello_Android.onCreate(Hello_Android.java:17)

E/AndroidRuntime(  180): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

E/AndroidRuntime(  180): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
What is the most interesting to me is the null pointer exception at line 17 of Hello_Android.java, the line in question being:

Code:
button.setOnClickListener(new OnClickListener(){
I am not sure how this is throwing a null pointer exception though... any help would be appreciated.

Last edited by pocnib; 02-08-2010 at 05:13 AM.. Reason: SOLVED
pocnib is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to view run time application exception androiddev Android Development 0 02-02-2010 06:55 AM
Not able to call button.onclickListener Anjul Android Answers 1 01-02-2010 09:32 PM
Yet another problem with button layout... rvaul Android Development 2 11-28-2009 05:07 PM
Q. Update all application/button? Is there… matt172001 Android Applications 0 11-18-2009 07:19 PM
Volume button switching? justinrobert Android Hardware 2 03-12-2009 06:35 PM


Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone | Android Phone

All times are GMT. The time now is 10:26 AM.



SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2008 TalkAndroid.com. All rights reserved.