user1588307
user1588307

Reputation: 9

OnClick event is not starting a new activity

I've created a button in my xml layout and followed with an onClick event to handle the button to start a new activity. For some reason when I click the button the app crashes. Any ideas why?

I've used this approach to creating new activities with buttons before with success. I'm Unsure why this is not working.

private Button view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stretchHow);
    view = (Button)findViewById(R.id.perfect);
    view.setOnClickListener(phase);
}

View.OnClickListener phase = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(view.getId() == ((Button)v).getId()){
        Intent i = new Intent(Stretch.this, Perform.class);
        startActivity(i);
        }
    }
};

This is the error I get.

09-28 23:28:37.364: E/AndroidRuntime(275): FATAL EXCEPTION: main
09-28 23:28:37.364: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.proj.fitness/org.proj.fitness.Perform}: android.content.res.Resources$NotFoundException: Resource ID #0x7f060073 type #0x12 is not valid
09-28 23:28:37.364: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-28 23:28:37.364: E/AndroidRuntime(275):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

Upvotes: 0

Views: 875

Answers (3)

Deep Verma
Deep Verma

Reputation: 842

Try removing the R file from the gen Eclipse folder. It will then be regenerated and the problem hopefully resolved.

Upvotes: 1

ColdSharper
ColdSharper

Reputation: 84

This is the key line of your error:

android.content.res.Resources$NotFoundException: Resource ID #0x7f060073 type #0x12 is not valid

It is possible the event is firing but that the target is not there, or where the handler expects it to be. Do keep checking your methods, but also make sure you do not have a legitimate "not found" situation.

Upvotes: 0

chris-tulip
chris-tulip

Reputation: 1830

I think that the intent is getting the wrong context since Stretch.this shouldn't be contained inside the onClickListener. You can try moving it out to another function called startPerform in your activity so it would look like so:

View.OnClickListener phase = new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(view.getId() == ((Button)v).getId()){
    Intent i = new Intent(Stretch.this, Perform.class);
    startActivity(i);
    }

}
};

public void startPreform () 
{
     startActivity(new Intent(this, Perform.class));
}

Upvotes: 0

Related Questions