Android Multiple Intents - One Form
Solution 1:
@Meryl2 When u are using ""android:onClick="btnLogout"""
Then you should have corresponding method public void btnLogout(View view{ //your code here }
Same applies for all buttons in your code
Solution 2:
Your Mistakes ->
You have not used setcontentView()
nor you overridden onCreate()
method. One more thing, you are not sending intent
correctly.
Here is how you can send intent
Intent intent = newIntent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
Solution 3:
I have modified the class and the layout. You need to create the other classes for each action.
publicclassMainActivityextendsActivityimplementsButton.OnClickListener{
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@OverridepublicvoidonClick(View v) {
intid= v.getId();
Intentintent=null;
if(id == R.id.btnHockey) {
intent = newIntent(MainActivity.this, Hockey.class);
} elseif(id == R.id.btnCurling) {
intent = newIntent(MainActivity.this, Curling.class);
} elseif(id == R.id.btnFootball) {
intent = newIntent(MainActivity.this, Football.class);
} elseif(id == R.id.btnLogout) {
intent = newIntent(MainActivity.this, Logout.class);
} elseif(id == R.id.btnLacrosse) {
intent = newIntent(MainActivity.this, Lacrosse.class);
}
startActivity(intent);
}
}
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/imageView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"/><Buttonandroid:id="@+id/btnFootball"android:layout_below="@id/imageView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:text="Football"android:onClick="onClick" /><Buttonandroid:id="@+id/btnHockey"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/btnFootball"android:layout_marginTop="25dp"android:text="Hockey"android:onClick="onClick" /><Buttonandroid:id="@+id/btnLacrosse"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/btnHockey"android:layout_marginTop="25dp"android:text="Lacrosse"android:onClick="onClick" /><Buttonandroid:id="@+id/btnCurling"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/btnLacrosse"android:layout_centerHorizontal="true"android:layout_marginTop="25dp"android:text="Curling"android:onClick="onClick" /><Buttonandroid:id="@+id/btnLogout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/btnCurling"android:layout_centerHorizontal="true"android:layout_marginTop="25dp"android:text="Logout"android:onClick="onClick" />
Solution 4:
I think i got your problem. In each Button you set the name of the method that will be used when you click on it :
android:onClick="btnCurling"
android:onClick="btnHockey"
...
But you only have one method here which is :
publicvoidButtonOnClick(View v){
...
}
One solution is to define each method like so :
publicclassMainMenuextendsActivity {
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
}
// it will get there when you click on the "@+id/btnHockey" ButtonpublicvoidbtnHockey(View v) {
Intentintent=newIntent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
}
// then you add the other ones btnCurling, ...
}
Another solution is to set the Listener of each button programmatically :
publicclassMainMenuextendsActivityimplementsView.OnClickListener {
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
// This will find your button in the layout you just set // and then set this Class as your Listener
findViewById(R.id.btnFootball).setOnClickListener(this);
// Then set the listener of all your other buttons
}
@OverridepublicvoidonClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
Intentintent=newIntent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
case R.id.btnHockey:
Intentintent=newIntent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
// ...
}
}
}
You also have to remove all the android:onClick="btnCurling"
line in your XML Layout file for this solution to work.
Hope it helps cheers :)
Solution 5:
Try this code;
publicclassMainMenuextendsActivityimplementsOnClickListener {
Button btn_football,btn_hokey,btn_something; // add another button here@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btn_football = (Button)findViewById(R.id.btnFootball);
// add another button here same way
btn_football.setOnClickListener(this);
// add another button here same way @OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubswitch(v.getId()){
case R.id.btnFootball:
Intentintent=newIntent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
case R.id.another button:
Intentintent=newIntent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
//.......add here another button with same way
}
}
}
Post a Comment for "Android Multiple Intents - One Form"