简体   繁体   中英

launching a new activity in Android

I am trying to learn screen navigation in android

I am trying to achieve as follows

  • when i click on button in activity it launches a new activity
  • but that end activity must contain a back button when on click it should return the first activity

ill post some the descriptive code below

CopperchimneyDesc.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.copperchimney_desc_screen);


Button PHOTOBUTTON=(Button) findViewById(R.id.CopperChimneyPhotosButton);
            PHOTOBUTTON.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent pht=new Intent(CopperChimneyDesc.this,CopperChimneyPhotos.class);
                    startActivity(pht); 
                }
            });

CopperChimneyPhotos.java

public class CopperChimneyPhotos extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.copper_chimney_photos);


        Button btn=(Button) findViewById(R.id.PhotoButton);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent pt=new Intent(CopperChimneyPhotos.this,CopperChimneyDesc.class);
                startActivity(pt);
            }
        });
    }

PS:: i have declared the new activity in manifest

my error log is as follows::

08-16 17:42:33.730: E/AndroidRuntime(444): FATAL EXCEPTION: main
08-16 17:42:33.730: E/AndroidRuntime(444): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.findmybuffet/com.project.findmybuffet.CopperChimneyDesc}: java.lang.NullPointerException
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.os.Looper.loop(Looper.java:123)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 17:42:33.730: E/AndroidRuntime(444):  at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:42:33.730: E/AndroidRuntime(444):  at java.lang.reflect.Method.invoke(Method.java:507)
08-16 17:42:33.730: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 17:42:33.730: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 17:42:33.730: E/AndroidRuntime(444):  at dalvik.system.NativeStart.main(Native Method)
08-16 17:42:33.730: E/AndroidRuntime(444): Caused by: java.lang.NullPointerException
08-16 17:42:33.730: E/AndroidRuntime(444):  at com.project.findmybuffet.CopperChimneyDesc.onCreate(CopperChimneyDesc.java:39)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 17:42:33.730: E/AndroidRuntime(444):  ... 11 more

Any ideas on how to overcome this, i have pasted relevent code .

you just call finish() method..

btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            finish();
        }
    });

in your second activity

Try something like this:

Intent goToMainScreen = new Intent("com.dereaper.goalsmanager.MAINSCREEN");
startActivity(goToMainScreen);
finish(); 

Call finish only if you want the activity to quit after moving to the next one.

try like this: call in onclick methed:

finish();
System.gc();

to finish the activity and come back to previous activity

There must be something wrong with your CopperChimneyDesc.class. Check out the onCreate function. Especially on line 39. There is a nullpointer

The return function:

@Override
public void onClick(View v) {
   if(v == PHOTOBUTTON){
       // TODO Auto-generated method stub
       Intent pht=new Intent(CopperChimneyDesc.this,CopperChimneyPhotos.class);
       startActivity(pht); 
   }
   if(v == OTHERBUTTON){
       finish();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM