user2661663
user2661663

Reputation: 277

Can't start another activity in Android

I'm learning android developpement, and I wrote a short and easy code, but it doesn't work. I can't start another activity,despite many try ! Here is the code of the main activity :

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



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.pageaccueil, menu);
    return true;
}
public void onCreate1(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_pageaccueil);

  final Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    Intent intent = new Intent(Pageaccueil.this, Devise.class);
    startActivity(intent);
    }
});
   }
}

And the button part of the XML Layout from the first/main activity :

    <Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="38dp"
    android:text="convertisseur de devises"
  />

The second activity is "devise", and here is its code : import android.os.Bundle; import android.app.Activity; import android.view.Menu;

 public class Devise extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.devise, menu);
    return true;
}

}

Does anyone know How can I make the second activity to launch ? I tried many times without any success .

Thank you in advance !!

Upvotes: 1

Views: 1250

Answers (1)

Tarsem Singh
Tarsem Singh

Reputation: 14199

use Following in your onCreate() instead of onCreate1()

final Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    Intent intent = new Intent(Pageaccueil.this, Devise.class);
    startActivity(intent);
    }
});
   }

also study the Life Cycle of Activity http://developer.android.com/training/basics/activity-lifecycle/index.html

Upvotes: 2

Related Questions