问题描述
||
这是我在该网站上的第一篇文章,我将尽力按照要求的技巧进行具体说明。我正在使用带有ADT 10.0.1的Eclipse Helios
我一直在研究一个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
}
});
所以我的问题是,如何获得布局前菜单上的按钮以链接到它们应该在的位置?为了使它正常运行,是否有任何语法错误或需要添加到此代码段的内容?
提前致谢!
解决方法
好吧,让我首先说:这不是android sdk的意思。您必须针对每屏内容的更改进行一次活动,这可以通过在此处切换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中调用)。
第三:好吧...的确,每屏内容都要进行一项活动:)