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)
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: