简体   繁体   中英

Android Multiple Intents - One Form

Good afternoon,

I am trying to create a basic menu in my andorid application which contains 5 buttons each bringing you to another form. I am trying to create the java to carry out this action but appear to be running into the following error with each of my buttons

"EXAMPLE cannot be resolved as a variable"

Please help me in a solution to my code or if there is a simpler way to allow me to execute this menu with 5 buttons each going to a different form

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.techblogon.loginexample.MainMenu" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/pic" />

    <Button
        android:id="@+id/btnFootball"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Football"
        android:onClick="btnFootball" />

    <Button
        android:id="@+id/btnHockey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnFootball"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Hockey"
        android:onClick="btnHockey" />

    <Button
        android:id="@+id/btnLacrosse"
        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="Lacrosse"
        android:onClick="btnLacrosse" />

    <Button
        android: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="btnCurling" />

    <Button
        android: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="btnLogout" />

</RelativeLayout>

Here is the Java:

package com.techblogon.loginexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;


public class MainMenu extends Activity {

    public void ButtonOnClick(View v) {
        switch (v.getId()) {
          case R.id.btnFootball:
            startActivity(Football);
            break;
          case R.id.btnHockey:
              startActivity(Hockey);
            break;
          case R.id.btnLacrosse:
              startActivity(Lacrosse);
                break;
          case R.id.btnCurling:
              startActivity(Curling);
                break;
          case R.id.btnLogout:
              startActivity(HomeActivity);
                break;
          }
    }


    }

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 = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);

I have modified the class and the layout. You need to create the other classes for each action.

public class MainActivity extends Activity implements Button.OnClickListener{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

@Override
public void onClick(View v) {
    int id = v.getId();

    Intent intent = null;
    if(id == R.id.btnHockey) {
        intent = new Intent(MainActivity.this, Hockey.class);
    } else if(id == R.id.btnCurling) {
        intent = new Intent(MainActivity.this, Curling.class);
    } else if(id == R.id.btnFootball) {
        intent = new Intent(MainActivity.this, Football.class);
    } else if(id == R.id.btnLogout) {
        intent = new Intent(MainActivity.this, Logout.class);
    } else if(id == R.id.btnLacrosse) {
        intent = new Intent(MainActivity.this, Lacrosse.class);
    }

    startActivity(intent);
}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<Button
    android: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" />

<Button
    android: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" />

<Button
    android: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" />

<Button
    android: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" />

<Button
    android: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" />

@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

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 :

public void ButtonOnClick(View v) {
    ...
}

One solution is to define each method like so :

public class MainMenu extends Activity {

    protected void onCreate(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" Button
    public void btnHockey(View v) {
        Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
        startActivity(intent);
    }

    // then you add the other ones btnCurling, ...
}

Another solution is to set the Listener of each button programmatically :

public class MainMenu extends Activity implements View.OnClickListener {

    protected void onCreate(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
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnFootball:
                Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
                startActivity(intent);
                break;
            case R.id.btnHockey:
                Intent intent = new Intent(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 :)

Try this code;

public class MainMenu extends Activity implements OnClickListener {
Button btn_football,btn_hokey,btn_something; // add another button here
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.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 
   @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.btnFootball:
        Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
        startActivity(intent);
        break;
    case R.id.another button:
      Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
       startActivity(intent);  
    break;
    //.......add here another button with same way
 }

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM