Eclipse Helios (ADT 10.0.1) 中的 Android 按钮无法正确链接

发布于 2024-11-10 15:03:46 字数 3856 浏览 3 评论 0原文

这是我在该网站上发表的第一篇文章,我将尽力按照要求的提示进行具体说明。我正在使用 eclipse Helios,使用 ADT 10.0.1

我一直在开发一个 Android 应用程序,该应用程序应该是《星际争霸 II》的一般指南。非常基本的东西,用于编程课程。它应该有一个带有继续按钮的介绍背景,链接到主菜单。

主菜单包含一些按钮,这些按钮都应链接到创建的不同布局。当我在模拟器中启动应用程序时(我尝试了级别 12、9 等),第一个按钮链接到菜单,但菜单上的按钮无法链接。我的代码中没有语法错误,但是它确实显示了除第一个按钮之外的所有按钮的整个黄色下划线。我断断续续地摆弄了基本语法,让它不显示任何红色或黄色下划线,但它也没有让我有任何进展。 当我从代码中删除最后一个按钮(布局中的后退按钮)链接回前菜单时,菜单上的按钮开始链接到最后一个按钮的链接,我的意思是代码中的最后一个按钮。所以我认为它可能会跳过所有其他侦听器,只使用代码中最后一个按钮的侦听器,或者类似的东西。请记住,我还不太擅长编程。

这是代码的基本外观。

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
            }
        });

所以我的问题是,如何让布局前端菜单上的按钮链接到它们应该链接到的位置?是否存在任何语法错误或我需要添加到此代码片段以使其正常运行的内容?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2024-11-17 15:03:46

好吧,首先让我说:这不是 android sdk 的意思。你必须为一屏内容的每次变化创建一个 Activity,这是通过此处的 contentview 切换来实现的。如果您还没有这样做,也许可以看看这里: http://developer.android.com /guide/topics/fundamentals.html

正确的方式(带有活动),用户可以使用系统后退按钮返回,因此基本上不需要有后退按钮,并且用户会期望系统后退按钮像这样工作。现在,按系统后退按钮会导致应用程序“关闭”并显示之前打开的内容。

但是,可以像您尝试的那样进行操作。这是一个简短的片段:

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);
}

}

你看,你的代码有很大的不同。代码中的第一个错误是将点击侦听器仅绑定到第一个按钮(可能复制/粘贴然后忘记)。

其次,如果你想像这样解决它,你必须手动膨胀你的布局。否则你会到处得到空指针,因为这些布局及其子元素(按钮等)是延迟实例化的(仅当需要它们时,例如在 setcontentview 中调用)。

第三:好吧……真的,为你的应用程序的每一屏内容执行一个 Activity :)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文