my first activity xm
<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="#a5c63b" 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=".mainactivity" > <button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="70dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="70dp" android:background="#434d23" android:text="ok continue" android:onclick="frontt" android:textcolor="#a5c63b" /> <imageview android:id="@+id/imageview11" android:layout_width="250dp" android:layout_height="250dp" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:src="@drawable/question" /> <textview android:id="@+id/txtview11" android:layout_width="match_parent" android:layout_height="20dp" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="loading..." android:textsize="12dp" /> </relativelayout> this second activity xml
<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="#a5c63b" 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=".mainactivity" > <button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="70dp" android:onclick="onclick" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="70dp" android:background="#434d23" android:text="ok" android:textcolor="#a5c63b" /> <edittext android:id="@+id/txtedit" android:layout_width="match_parent" android:layout_height="20dp" android:layout_alignleft="@+id/button1" android:layout_alignparenttop="true" android:layout_margintop="50dp" android:background="#edf2db" android:ems="10" android:inputtype="numberdecimal" /> <imageview android:id="@+id/imageview1" android:layout_width="250dp" android:layout_height="250dp" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:src="@drawable/question" /> <textview android:id="@+id/txtview" android:layout_width="match_parent" android:layout_height="20dp" android:layout_alignbottom="@+id/txtedit" android:layout_alignleft="@+id/txtedit" android:layout_marginbottom="21dp" android:text="guess single digit number" android:textsize="12dp" /> <textview android:id="@+id/txtview2" android:layout_width="match_parent" android:layout_height="20dp" android:layout_alignleft="@+id/btn" android:layout_below="@+id/btn" android:text="textview" /> </relativelayout> this first java file code
package com.example.game; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.app.activity; import android.content.intent; import android.view.menu; import android.widget.button; public class mainactivity extends activity { mediaplayer btnsound; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.front); button next = (button) findviewbyid(r.id.btn1); btnsound = mediaplayer.create(mainactivity.this,r.raw.game); btnsound.start(); next.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent nextscreen = new intent(getapplicationcontext(), copyofmainactivity.class); startactivity(nextscreen); //finish(); } } ); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } } this second java file
package com.example.game; import java.util.random; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.textview; import android.app.activity; import android.view.menu; public class copyofmainactivity extends activity { mediaplayer btnsound; random random = new random(); int randnumber = random.nextint(10); button b2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btnsound = mediaplayer.create(copyofmainactivity.this,r.raw.game); btnsound.start(); b2= (button) findviewbyid(r.id.btn); b2.setonclicklistener(new onclicklistener() { public void onclick(view v) { edittext input = (edittext)findviewbyid(r.id.txtedit); textview resulttext = (textview) findviewbyid(r.id.txtview2); string inputstring = input.gettext().tostring(); int number = integer.parseint(inputstring); if(randnumber==number) { resulttext.settext("you win" ); imageview image = (imageview) findviewbyid(r.id.imageview1); image.setimageresource(r.drawable.win); } else if (number>randnumber) { resulttext.settext("you guess high number" ); imageview image = (imageview) findviewbyid(r.id.imageview1); image.setimageresource(r.drawable.tryagain); } else if (number<randnumber) { resulttext.settext("you guess low number" ); imageview image = (imageview) findviewbyid(r.id.imageview1); image.setimageresource(r.drawable.lose); } } } ); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } } i have 1 button on first activity xml layout .the first activity layout display after run application whenever click on button on first xml first want go on next laytout after clicking error show in logcat tab , application stop working.
androidruntime @ dalvik.system.nativestart.main(native method)
what should do?
please add both activities manifest file if missed out.
<application> <activity android:name="activity1"> </activity> <activity android:name="activity2"> </activity>
Comments
Post a Comment