0

This is my first post on the site, and I'll try to be just as specific as the tips requested. I'm using eclipse Helios, with the ADT 10.0.1

I've been working on an Android application which is supposed to be a general guide of Starcraft II. Pretty basic stuff, it's for a programming class. It's supposed to have an intro background with a continue button, linking to the main menu.

The main menu consists a few buttons that should all link to different layouts created. When I start my application in an emulator (I tried level 12, 9 etc.) the first button links to the menu, but the buttons on the menu fail to link. I have no syntax errors in my code, however it does show the whole yellow underline for all the buttons except the first one. I've fiddled with the basic syntax a bit on and off to get it to not display any red or yellow underlines, and it didn't get me anywhere either. When I removed the last button from the code, which is a back button from the layouts, linking back to the front menu, the buttons on the menu instead started linking to the last button's link, by which I mean the last one in the code. So I thought it might be skipping all of the other listeners and just using the one for the last button in the code, or something like that. Bear in mind I'm not very good at programming yet.

Here's the basic look of the code.

package lol.lol;

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

public class ofk extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle button1) {
        super.onCreate(button1);

        setContentView(R.layout.intro);

        Button button = (Button) findViewById(R.id.continuebutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.frontmenu);

            }
        }); 




    final Button button2 = (Button) findViewById(R.id.aboutapp);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.aboutsc2g);
            }
        }); 




        final Button button3 = (Button) findViewById(R.id.aboutsc);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.aboutsc2);
                // Perform action on click
            }
        });

        final Button button4 = (Button) findViewById(R.id.micro);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.micro);
                // Perform action on click
            }
        });

        final Button button5 = (Button) findViewById(R.id.macro);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.macro);
                // Perform action on click
            }
        });

        final Button button6 = (Button) findViewById(R.id.mechanics);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.mechanics);
                // Perform action on click
            }
        });

        final Button button7 = (Button) findViewById(R.id.zergbasics);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.zergbasics);
                // Perform action on click
            }
        });

        final Button button8 = (Button) findViewById(R.id.terranbasics);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.terranbasics);
                // Perform action on click
            }
        });

        final Button button9 = (Button) findViewById(R.id.protossbasics);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.protossbasics);
                // Perform action on click
            }
        });

        final Button button10 = (Button) findViewById(R.id.backbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.frontmenu);
                // Perform action on click
            }
        });

So my question being, how do I get the buttons on the layout frontmenu to link where they're supposed to? Are there any syntax errors or things I need to add to this code snippet to make it function properly?

Thanks in advance!

user773908
  • 1
  • 1
  • 2

1 Answers1

0

well, let me first say: this is not the way it was meant by the android sdk. you gotta have an activity for every change of a screenful of content, which is realized by switching of contentview here. maybe look here, if you haven´t done so: http://developer.android.com/guide/topics/fundamentals.html

the right way (with activities), it´s possible for the users to go back with the system-backbutton, so there´s basically no need to have a backbutton, and users will expect the system-backbutton to work like that. now, pressing the system-backbutton causes the app to 'close' and to show what was opened before.

however, it is possible to do it like you tried it to. here´s a short snippet:

public class Start extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {

LayoutInflater mLayoutInflater = getLayoutInflater();
final LinearLayout mLinearLayoutIntro = (LinearLayout) mLayoutInflater.inflate(R.layout.intro, null);

setContentView(mLinearLayoutIntro);

final LinearLayout mLinearLayoutFrontMenu = (LinearLayout) mLayoutInflater.inflate(R.layout.frontmenu, null);

Button continueButton = (Button) findViewById(R.id.continuebutton);
continueButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    setContentView(mLinearLayoutFrontMenu);

    }
});

final LinearLayout mLinearLayouAboutSc2g = (LinearLayout) mLayoutInflater.inflate(R.layout.aboutsc2g, null);

final Button aboutAppButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.aboutapp);
aboutAppButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

    setContentView(mLinearLayouAboutSc2g);
    }
});

final Button backButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.backbutton);
backButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

    setContentView(mLinearLayoutIntro);
    // Perform action on click
    }
});
super.onCreate(savedInstanceState);
}

}

you see, there´s quite a bit difference to your code. first error in your code was to bind the click listeners only to the first button (possibly copy/pasted and then forgot).

second, if you want to solve it like this, you have to manually inflate your layouts. otherwise you will get null pointers everywhere, because these layouts and its childs (buttons etc) are instantiated lazily (only when they are needed e.g. called in setcontentview).

and third: well...really, do your app with one activity for every screenful of content :)

stk
  • 6,311
  • 11
  • 42
  • 58
  • Thank you SO much! I've been trying tons of small things. And yes, the whole thing about listeners being bound to "button" only is just a copypaste error into here, I fiddled with it a ton, aswell as the ID's for the buttons themselves. I actually also tried to give every button an activity of it's own, but when I tried to enclose them in the way I thought was correct I got syntax errors I couldn't solve. Again, extremely thankful for your input as this project has gotten me really really interested in programming as a whole! Love ya! :D – user773908 May 29 '11 at 14:03
  • Tried implementing the code you offered and screwed around with it a bit to no result. Most often the emulator gets stuck on ActivityManager starting intent or something like that. I also tried making a separate class for the second button in my original code, and implementing another activity to the xml, but it wouldn't start up either. I'm at a complete loss here.. – user773908 May 29 '11 at 22:25
  • take a look here: http://dl.dropbox.com/u/1831328/Test1.zip and don´t forget: it is NOT meant to be coded like that! instead, have a look on intents and create many several activities. – stk May 30 '11 at 12:10
  • Thank you so much for your help! You've been a godsend. Basically I ran out of time, so I made a simple webview that shows YouTube and documented my problem solving of this app, to prove my abilities, and it should easily get me an A in the course. LOVE YOU :D – user773908 May 31 '11 at 20:34
  • Happy to help...but now you could do me a favor and accept my answer plus maybe upvote it :-) cheers – stk May 31 '11 at 23:36