• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

New to coding! Going to pull hair out! Help!

11eleven

Lurker
With that said, I have written a droid app for the company I work for, although I've never worked in java before. All the components/activities work fine, except one thing:

On the main activity, I have 4 buttons to click to 4 other activities. The buttons work, BUT YOU HAVE TO CLICK THE BUTTONS IN ORDER (top to bottom) to get each click to summon the new activities. Once all 4 activities have been clicked (and back button clicked to get back to the main activity), then the buttons work fine in any order.

WHAT THE HECK DID I DO WRONG?? I've searched tutorials, forums, and basically the entire internet, and it appears I'm the only one to ever have this problem. I'm not sure if my java is wrong, but it shows no errors. Here is the java for the main activity below.

PLEASE HELP - I'M GOING TO STAB MYSELF IN THE NECK IF I CAN'T FIGURE THIS OUT! Seriously, it's very frustrating.

package com.acrrestores.droidapp1;

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

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button b1 = (Button) findViewById(R.id.buttonMain1);
b1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
startActivity(new Intent(Main.this,emergency_response.class));

Button b2 = (Button) findViewById(R.id.buttonMain3);
b2.setOnClickListener(new OnClickListener () {

public void onClick(View v) {
startActivity (new Intent(Main.this, acr_services.class));

Button b3 = (Button) findViewById(R.id.buttonMain2);
b3.setOnClickListener(new OnClickListener () {

public void onClick(View v) {
startActivity (new Intent(Main.this, service_territory.class));

Button b4 = (Button) findViewById(R.id.buttonMain4);
b4.setOnClickListener(new OnClickListener () {

public void onClick(View v) {
startActivity (new Intent(Main.this, acrcontacts.class));
}
});

}
});

}
});

}
});
}}
 
You are defining what each button does inside the previous button's "onClick" method. And implementing OnClickListeners incorrently. Try this:


package com.acrrestores.droidapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

//Make your Main activity implement OnClickListener. Now we can use "this" as our parameter to setOnClickListener.

public class Main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//Now we create views and cast them as buttons since onClick() takes a View object, and give them onClickListeners.

View b1 = (Button) findViewById(R.id.buttonMain1);
b1.setOnClickListener(this);

View b2 = (Button) findViewById(R.id.buttonMain3);
b2.setOnClickListener(this);

View b3 = (Button) findViewById(R.id.buttonMain2);
b3.setOnClickListener(this);

View b4 = (Button) findViewById(R.id.buttonMain4);
b4.setOnClickListener(this);
}

//I recommend using a "switch" statement to keeps things simple to call new activities.
//We only need to declare one intent. In this example we will name it "newview".
//Depending on what button we click, newview can have one of four values.
//Keep in mind that you only need one onClick() method in each activity.

public void onClick(View v) {
Intent newview;
switch(v.getId())
{
case R.id.buttonMain1:
newview = new Intent(this, emergency_response.class);
startActivity(newview);
break;
case R.id.buttonMain2:
newview=new Intent(this,acr_services.class);
startActivity(newview);
break;
case R.id.buttonMain3:
newview=new Intent(this,service_territory.class);
startActivity(newview);
break;
case R.id.buttonMain4:
newview=new Intent(this,acrcontacts.class);
startActivity(newview);
break;
}

}
}


Note that i did not test this code out, but i did use the same context i use when i program. Sorry if there is a spelling mistake. I didnt use
Code:
 tag because it made it undreadable.
 
Back
Top Bottom