Newbie doubt here! Please advise.

by Vijay » Fri, 20 Feb 2009 13:22:16 GMT


Sponsored Links
 Hi,
I just started programmind an application using Eclipse for Android.
I am creating 2 screens.

1st screen:
Has a text and
a button which onClick --> takes you to the 2nd screen.

2nd screen:
Has a text and
a button which onClick -- > takes you to the 1st screen.

just like NEXT and BACK.

But somehow i keep getting "Application Simple application has stopped
unexpectedly." Force Close.


My code:Simple.java

package com.vijay.simple;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class Simple extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button b1 = (Button) findViewById(R.id.button_id1);
    b1.setOnClickListener(buttonListener1);

    Button b2 = (Button) findViewById(R.id.button_id2);
    b2.setOnClickListener(buttonListener2);
    }

    private OnClickListener buttonListener1 = new OnClickListener(){
        public void onClick(View v1){
                setContentView(R.layout.main_2);
        }
    };

    private OnClickListener buttonListener2 = new OnClickListener(){
        public void onClick(View v2){
                setContentView(R.layout.main);
        }
    };

}




--~--~---------~--~----~------------~-------~--~----~



Newbie doubt here! Please advise.

by lorenz » Sat, 21 Feb 2009 01:49:29 GMT


 Hey

The problem is that your reference b2 is null and you get a
NullPointerException when trying to register the OnClickListener on
it. This happens because b2 does not yet exist when you ask for it by
calling findViewById(R.id.button_id2), for only the contents of your
main.xml file is already loaded in the onCreate() method.

However, I'm not sure if you should handle two "pages" in the same
class, since these are actually two activities. Maybe you want to
consider writing two different activities and then the buttons can
start them using Activity.startActivity(Intent).

Hope this helps ;-)
Lorenz




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. How to access storage by using API?

If I want to read/write external storage, like usb drive,
does Android provide this feature to do?

Or how do using Google API to read/write SDcard on Mobile?
Where to get sample code for this goal?

Thanks for your kindly help.
--~--~---------~--~----~------------~-------~--~----~

2. keypad stuff

Hi folks,

I just ported Linux and Android Donut to a new device, although I'm
still working on power management and a few other drivers, and had a
couple of questions.

1) Is there any sort of template for multi-tap keypads in the android
source? Possibly with auto-completion?

By multi-tap, I mean that each key has both a number and 3 letters,
and that you need to hit the key a certain number of times to get a
specific letter. This is what was on a lot of mobiles over the last
couple of decades.

2) Are the Android keypad requirements defined anywhere, in a nice,
clean list? (home key, back key, etc) Right now I can type as much as
I please, but I can't seem to get back to the previous screen or the
home screen, which has some disadvantages as you might guess ;-)

Cheers,

Chris

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

3. How to compile Android kernel with Ipv6?

4. memory leak in activity

5. How to launch my app if some one browses any xml file

6. Just for test, Please ignore

7. Browser issue when visiting YouTube