简体   繁体   中英

Multiple buttons w/ each selecting a different Activity

I am new to coding and am trying to work on a project when my main Activity page has a series of buttons. From these buttons I would like each to open a different activity or command. I been searching and found what I thought SHOULD work, however, it does not. I get a crash when it comes loading the app and then clicking on the button. Below is the code. Any pointers to show my mistake somewhere would be kindly appreciated.

package com.example.finalproject2;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

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

    //Initialize Buttons
    Button b1 = (Button) findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.button02);
    Button b3 = (Button) findViewById(R.id.button03);
    Button b4 = (Button) findViewById(R.id.button01);
    Button b5 = (Button) findViewById(R.id.button04);

    //Set OnCLickListeners
    b1.setOnClickListener(chicagoListener); 
    b2.setOnClickListener(sanJoseListener); 
    b3.setOnClickListener(baltimoreListener); 
    b4.setOnClickListener(westPalmBeachListener);   
    b5.setOnClickListener(websiteListener); }

private OnClickListener chicagoListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, Chicago.class));
    }
};
private OnClickListener sanJoseListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, SanJose.class));
    }
};

private OnClickListener baltimoreListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, Baltimore.class));
    }
};
private OnClickListener westPalmBeachListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
    }
};
private OnClickListener websiteListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
    }
};




        {
}





}

Mention your all activity in your app manifesto file (like this):

<activity
      android:name="com.example.finalproject2.Chicago"
      android:label="@string/app_name" >
</activity>

And try to do somthing like this:

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


    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 
    b4.setOnClickListener(this);   
    b5.setOnClickListener(this);
}



@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.button1:
        startActivity(new Intent(MainActivity.this, Chicago.class));
        break;

    case R.id.button02:
        startActivity(new Intent(MainActivity.this, SanJose.class));
        break;

    case R.id.button03:
        startActivity(new Intent(MainActivity.this, Baltimore.class));
        break;

    case R.id.button01:
        startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
        break;

    case R.id.button04:
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
        break;  

    }
}

Button - Android developer

sample from developer site

    Button button = (Button) findViewById(R.id.button_send);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         // Do something in response to button click
        }
    });

another good example of how to use onclick

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