redhatab wrote:
> Hi,
> 
> Am a total beginner and I was trying out to extrapolate on the Hello
> Android code, but I've sort of hit a road block with a null pointer
> exception. The code is as below:
> 
> public class HelloAndroid extends Activity
> {
>          @Override
>          public void onCreate(Bundle icicle)
>          {
>              super.onCreate(icicle);
> 
>              setContentView(R.layout.screen1);
> 
>              Button btn = (Button) findViewById(R.id.hellobtn);
>                btn.setOnClickListener(new View.OnClickListener() {
>                    public void onClick(View view) {
>                        setContentView(R.layout.screen2);
>                    }
>                });
>          }
> }
> 
> As soon as I run this, the emulator gives me a null pointer exception.
> 
> The res/layout/screen1.xml and res/layout/screen2.xml are working
> perfectly fine because when I just run
> setContentView(R.layout.screen1) without the onClickListener, both the
> screens work fine.
> 
> I suppose the null pointer exception pops in the onClick function.

If you get the exception as soon as you run the program in the emulator, 
it can't be *inside* onClick(), as you would not have clicked anything yet.

More likely, btn is not getting set, because findViewById() is returning 
null, which would occur if R.id.hellobtn isn't in R.layout.screen1. The 
fact that your code compiles means that R.id.hellobtn probably exists 
somewhere...maybe in screen2?

Use adb logcat (or the Eclipse equivalent) to get a stacktrace to see 
where you're failing.

If you can't find the problem, post the XML layouts associated with this 
activity, and the stacktrace, and that may give us more clues.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Warescription: All titles, revisions, & ebook formats, just $35/year

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to