Reputation: 105
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/")));
}
};
{
}
}
Upvotes: 0
Views: 63
Reputation: 849
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;
}
}
Upvotes: 1
Reputation: 91
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
Upvotes: 0