我是新来的,我认为我在Java中犯了一个错误,但我不知道如何纠正它。大多数有类似问题的人都有更复杂的项目,我无法通过查看他们的代码来解决我的问题。
我想使用不同的按钮(其中9个)开始不同的活动,但是当我开始添加第二个按钮时,只弹出了活动1(LeftArmActivity)。无论我在XML中改变了什么来调用HeadActivity的正确启动方法,只有LeftActivity启动。我已经从其他主题中得到了一些暗示,这可能是由于覆盖了意图造成的,但我不知道如何解决这个问题。我试图使用getActivity()但它只是崩溃了。你能帮帮我吗?
@UPDATE
好的,我使用了下面推荐的开关,但现在应用程序根本没有开始:/
public class MainActivity extends AppCompatActivity {
Context context = this;
Button LeftArmOpener = (Button) findViewById(R.id.LeftArmOpener);
Button HeadOpener = (Button) findViewById(R.id.HeadOpener);
Button RightArmOpener = (Button) findViewById(R.id.RightArmOpener);
Button CreditsOpener = (Button) findViewById(R.id.CreditsOpener);
Button TrunkOpener = (Button) findViewById(R.id.TrunkOpener);
Button NextOpener = (Button) findViewById(R.id.NextOpener);
Button RightLegOpener = (Button) findViewById(R.id.RightLegOpener);
Button ExitOpener = (Button) findViewById(R.id.ExitOpener);
Button LeftLegOpener = (Button) findViewById(R.id.LeftLegOpener);
protected View.OnClickListener mClick;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.RightArmOpener: {
Intent i1 = new Intent(context, LeftArmActivity.class);
startActivity(i1);
break;
}
case R.id.HeadOpener: {
Intent i2 = new Intent(context, HeadActivity.class);
startActivity(i2);
break;
}
case R.id.LeftArmOpener: {
Intent i3 = new Intent(context, LeftArmActivity.class);
startActivity(i3);
break;
}
case R.id.CreditsOpener: {
Intent i4 = new Intent(context, CreditsActivity.class);
startActivity(i4);
break;
}
case R.id.TrunkOpener: {
Intent i5 = new Intent(context, TrunkActivity.class);
startActivity(i5);
break;
}
case R.id.NextOpener: {
Intent i6 = new Intent(context, NextActivity.class);
startActivity(i6);
break;
}
case R.id.RightLegOpener: {
Intent i7 = new Intent(context, RightLegActivity.class);
startActivity(i7);
break;
}
case R.id.ExitOpener: {
Intent i8 = new Intent(context, ExitActivity.class);
startActivity(i8);
break;
}
case R.id.LeftLegOpener: {
Intent i9 = new Intent(context, LeftLegActivity.class);
startActivity(i9);
break;
}
//create this for all 9 buttons
}
}
};
LeftArmOpener.setOnClickListener(mClick);
HeadOpener.setOnClickListener(mClick);
RightArmOpener.setOnClickListener(mClick);
CreditsOpener.setOnClickListener(mClick);
TrunkOpener.setOnClickListener(mClick);
NextOpener.setOnClickListener(mClick);
RightLegOpener.setOnClickListener(mClick);
ExitOpener.setOnClickListener(mClick);
LeftLegOpener.setOnClickListener(mClick);
}
}
答案 0 :(得分:1)
使用此
更新您的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openHead(View view){
startActivity(new Intent(MainActivity.this, LessonOne.class));
//startActivity(t);
}
public void openLeftArm(View view){
Intent i = new Intent(MainActivity.this, LeftArmActivity.class);
startActivity(i);
}
}
//问题是你正在调用startActivity()两次并从Actvity传递getActivity()。
答案 1 :(得分:0)
你说你有9个按钮所以我认为你应该在这种情况下使用 switch case ,请看下面的代码: -
以下是我的Button在xml中的显示方式,此处未使用android:onClick
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
本部分介绍活动: -
Context context = this;
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn9 = (Button) findViewById(R.id.btn9);
btn1.setOnClickListener(mClick);
btn2.setOnClickListener(mClick);
btn9.setOnClickListener(mClick);
View.OnClickListener mClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1: {
Intent i1 = new Intent(context, First.class);
startActivity(i1);
break;
}
case R.id.btm2: {
Intent i2 = new Intent(context, Second.class);
startActivity(i2);
break;
}
//create this for all 9 buttons
}
}
};
答案 2 :(得分:0)
在顶部声明你的按钮; (在oncreate方法之前)
Button yourbuttonname;
然后在oncreate方法上:
声明按钮的视图:
yourbuttonname = (Button) findViewById(R.id.buttonNameInYourXML);
yourbuttonname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ActualActivity.this, ActivityYouWantToGo.class);
intent.putExtra("tag",valueassociatedtotag); // if you want to pass some data
startActivity(intent)
}
});