The findViewById will return a non-null Widget object, only if you
first called setContentView and inflated the layout that contains it.
That is why nextButton works and backButton is null - giving you your
NullPointerException.

For a simple application, an easier way to create 2 pages, would be to
create 2 activities and switch between them.
http://developer.android.com/reference/android/app/Activity.html

Btw, there is related  interesting read at
http://android-developers.blogspot.com/2009/02/faster-screen-orientation-change.html


Balwinder Kaur
Open Source Development Center
·T· · ·Mobile· stick together

The views, opinions and statements in this email are those of the
author solely in their individual capacity, and do not necessarily
represent those of T-Mobile USA, Inc.


On Jul 22, 2:27 am, elegos <[email protected]> wrote:
> Hello there!
>
> I'm a newbie in Android programming, and was exploring the possibility
> to switch between more than - one - layouts (i.e. more pages?).
>
> So I've created a new XML layout page (called second.xml) and created
> some things in there. At least I've created a button to switch back to
> the first layout, but a NullPointerException fires up when the
> application starts, and that only for the "back" button, while the
> "next" button just works great.
>
> Any suggestion?
> Thanks :)
>
> package com.elegos.test;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.widget.Button;
> import android.widget.TextView;
>
> public class ListViewTest extends Activity {
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
>         TextView myText = (TextView)findViewById(R.id.myTextView);
>
>         Button nextButton = (Button)findViewById(R.id.ButtonNext);
>         Button backButton = (Button)findViewById(R.id.ButtonBack);
>
>         nextButton.setOnClickListener(getCListener(R.layout.second));
>         try {backButton.setOnClickListener(getCListener
> (R.layout.main));} catch(Exception e) {myText.append("\n"+e.toString
> ());}
>     }
>
>     final private OnClickListener getCListener(final int layout) {
>         return new Button.OnClickListener() {
>                 public void onClick(View v) {
>                         setContentView(layout);
>                 }
>         };
>     }
>
> }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to