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. Call to onDestroy() method of Service does not take place immediately from inside Activity




The doc for stopServices says: "Request that a given application service be
stopped." Key words being "request" and "stopped".


A service can be both started and have connections bound to it. In such a


My understanding: You're asking the system to stop the service, not to
destroy it. That will happen automatically, later, when it determines the
Service is no longer being used. For example, you could have another
activity that is bound to the service, so although it can be stopped, it
wouldn't be destroyed.

Why is this causing you a problem?

-------------------------------------------------------------------------------------------------
TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
transit tracking app for Android-powered devices

-- 

2. renaming a image file on sd card

i have to change the name of a jpg file , for that i have written the
following code

sdcard = Environment.getExternalStorageDirectory();
from = new File(sdcard,imagePath.substring(8).toString());
to = new File(sdcard,"/DCIM/RoseVille_"+Info.getText()
+"_"+Amount.getText()+"_"+timeStampFormat.format(n ew Date())+".jpg");

if(from.renameTo(to)==true)
{
/// if renamming is successfull;
}

this code creates a new file with the required name while the file
which i wanted to change is still their on sd card , if i remount the
sd card then the old file gets deleted and the renamed file remains
their.

so my problem is how to rename the file without remounting the sd card

thanks and regards
focode

-- 

3. Sang juara Foursquare dlm acara Launching Nexian Android

4. Facebook Quickly Releases Fix for Battery Drain Issues in App Update

5. - Call +62 di android CDM

6. How to know the home launcher app packagename through programming?

7. PV MediaRecorder native video recording error