Results 1 to 6 of 6
  1. #1
    Lion is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Issue with going "back" and multipule "links"

    Hi guys I have been working on a little project and I have come to a stop!

    I can not figure out how to go back to another page from another. The transaction works, user clicks the button and goes back to another page! But then none of the code works on that page. This is my code.

    Java Code:
    package com.example.space;
    
    
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		Button button1 = (Button) findViewById(R.id.Button1);
    		
    		button1.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				setContentView(R.layout.list);
    				
    				Button sol = (Button) findViewById(R.id.sol);
    				
    				Button sun = (Button) findViewById(R.id.sun);
    				
    				Button neb = (Button) findViewById(R.id.neb);
    				
    				Button milky = (Button) findViewById(R.id.milky);
    				
    				
    				sol.setOnClickListener(new OnClickListener() {
    					
    					@Override
    					public void onClick(View v) {
    						setContentView(R.layout.solar);
    					
    						Button solBack = (Button) findViewById(R.id.solBack);
    						
    						solBack.setOnClickListener(new OnClickListener() {
    							
    							@Override
    							public void onClick(View v) {
    							setContentView(R.layout.list);
    								
    							}
    						});
    					}
    				});
    				
    			}
    		});
    		
    	}
    
    
    }
    Another issue I am having is multiple buttons going to different pages on one page. I am sure you can see the page I am talking about with the four buttons, and only one listener. Where would I put what the other buttons should do? if I put them above each other I would guess that only one button will work?

    If the code is a bit messy that is me! I have come from many years with html css and php and I am trying to write out the Java without following a tut or something but I am stuck haha


    Thanks guys!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Issue with going "back" and multipule "links"

    It's a stylistic choice whether to have a single OnClickListener servicing multiple buttons or a separate listener for each button. If you decide to use one listener, its onClick(...) method can switch on the id of the View parameter.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Lion is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Issue with going "back" and multipule "links"

    So using setOnClickListener(new OnClickListener() is not the best choice? I am not able to work on my code this minute (at college) But will something like

    Java Code:
    sol.onclick=function(){
    setContentView(R.layout.solar);
    };
    This could be 100% as I just typed that out without reference I am not sure if =function should be there or replaced with another line! Also is it a must to have all the code in one file? Could I not split them up for each page?
    Last edited by Lion; 09-24-2013 at 01:15 PM. Reason: some spelling corrections

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Issue with going "back" and multipule "links"

    Oh... no. You can't assign functions like that. What I mean is you'll either do something like this:

    Java Code:
    OnClickListener onClickListener = new OnClickListener() { ... };
    button1.setOnClickListener(onClickListener);
    button2.setOnClickListener(onClickListener);
    button3.setOnClickListener(onClickListener);
    or this:

    Java Code:
    button1.setOnClickListener(new OnClickListener() { ... });
    button2.setOnClickListener(new OnClickListener() { ... });
    button3.setOnClickListener(new OnClickListener() { ... });
    Get in the habit of using standard Java naming conventions!

  5. #5
    Lion is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Issue with going "back" and multipule "links"

    Well it worked great at first but I am having a crashing issue, going from the main page to the "list" page crashes the app. I can not see anything at the minute but I am not too advanced! It must be an issue with the layout of the code?

    Java Code:
    package com.example.space;
    
    
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		Button button1 = (Button) findViewById(R.id.Button1);
    		
    		button1.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				setContentView(R.layout.list);
    				
    				Button sol = (Button) findViewById(R.id.sol);
    				
    				sol.setOnClickListener(new OnClickListener() { 
    					@Override
    					public void onClick(View v) {
    						setContentView(R.layout.solar);
    						
    					}
    				
    				});
    				
    				Button milky = (Button) findViewById(R.id.milky);
    				
    				milky.setOnClickListener(new OnClickListener() {
    					@Override
    					public void onClick(View v) {
    						setContentView(R.layout.milky);
    					}
    					
    				});
    				
    				Button sun = (Button) findViewById(R.id.sun);
    				sun.setOnClickListener(new OnClickListener() { 
    					@Override
    				public void onClick(View v) {
    					setContentView(R.layout.sunn);
    					
    				} });
    
    				Button neb = (Button) findViewById(R.id.neb);
    				neb.setOnClickListener(new OnClickListener() {
    					
    					@Override
    					public void onClick(View v) {
    						setContentView(R.layout.neb);
    						
    					}
    				});
    				
    			}
    		});
    		
    	}
    
    
    }
    Must be something stupid I have done somewhere.
    Last edited by Lion; 09-24-2013 at 11:03 PM.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Issue with going "back" and multipule "links"

    Posting the stack trace would go a long way toward helping people help you.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •