Changing an Activity in Android -
i'm learning android here, , java honest. i'm trying switch different activity via button click, however, keeps crashing. crashes when click button , go make switch. can please me figure i'm going wrong?
first activity:
package com.example.killacatoe; import android.os.bundle; import android.app.activity; import android.view.*; import android.widget.*; import android.content.*; public class tictactoe extends activity {//start tictacttoe class //constants //variables button mainbutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_tic_tac_toe); mainbutton = (button) findviewbyid(r.id.bplaynow); mainbutton.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub intent = new intent(getapplicationcontext(), playermenu.class); startactivity(i); } }); } }//end tictactoe class
activity i'm jumping to:
package com.example.killacatoe; import android.os.bundle; import android.app.activity; import android.view.*; import android.widget.*; public class playermenu extends activity { button boneplayer, btwoplayer; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.player); boneplayer = (button) findviewbyid(r.id.boneplayer); boneplayer.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub } }); btwoplayer = (button) findviewbyid(r.id.btwoplayer); btwoplayer.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub } }); } }
here xml first activity:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".tictactoe" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="30dp" android:textsize="50dp" android:text="welcome \ntic-tac-toe" android:textcolor="#ffffff" /> <button android:id="@+id/bplaynow" android:layout_width="150dp" android:layout_height="100dp" android:layout_below="@+id/textview1" android:layout_centerhorizontal="true" android:layout_margintop="137dp" android:textsize="34dp" android:text="play now!" /> </relativelayout>
xml second activity:
<?xml version="1.0" encoding="utf-8"?> <absolutelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" > /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="30dp" android:layout_x="78dp" android:layout_y="30dp" android:text="game mode" android:textcolor="#ffffff" android:textsize="50dp" /> <button android:id="@+id/boneplayer" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_x="25dp" android:layout_y="160dp" android:text="single player" android:textsize="25dp" /> <button android:id="@+id/btwoplayer" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_x="25dp" android:layout_y="220dp" android:text="two player" android:textsize="25dp" /> </absolutelayout>
for starting new activity, necessary add in manifest file.
<application > ... <activity android:name="com.example.killacatoe.playermenu" > </activity> </application>
then in oncreate(), starting new activity when button pressed, can do:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.player); boneplayer = (button) findviewbyid(r.id.boneplayer); boneplayer.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { intent intent = new intent(tictactoe.this, playermenu.class); startactivity(intent); } }); }
Comments
Post a Comment