class - Android - unexpected app crash -
ok have cocktail bible app lets select cocktail form list , should display various information cocktail text,img , button link youtube clip.the code compiling when click on 1 of items in list app unexpected crashes. @ moment app allows 2 cocktail's how run 20 cocktails? have removed imports way.
code run list class
package com.drunktxtapp; import android.app.activity; public class cocktailmenu extends activity { string classes[] = {"bloody_mary", "capirinha", "cosmopolitan", "cuba_libre", "daiquiri", "mai_tai", "manhattan", "margarita", "martini", "mint_julep", "mojito", "old_fashoned", "pina_colada", "screwdriver", "singapore_sling", "tom_collins", "whiskey_sour", "white_russian"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cocktail_menu); button b1 = (button) findviewbyid(r.id.bbylist); button b2 = (button) findviewbyid(r.id.brandomcocktail); b1.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { try { class<?> menuclass = class.forname("com.drunktxtapp.menu"); intent ourintent = new intent(cocktailmenu.this, menuclass); startactivity(ourintent); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); b2.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { string cocktailtype = classes[(int) (math.random() * classes.length)]; try{ class<?> ourclass = class.forname("com.drunktxtapp." + cocktailtype); intent openrandom = new intent(cocktailmenu.this, ourclass); startactivity(openrandom); }catch (classnotfoundexception e){ e.printstacktrace(); } }; }); } }
code produces list of cocktails
package com.drunktxtapp; import android.app.listactivity; public class menu extends listactivity{ string classes[] = {"bloody_mary", "capirinha", "cosmopolitan", "cuba_libre", "daiquiri", "mai_tai", "manhattan", "margarita", "martini", "mint_julep", "mojito", "old_fashoned", "pina_colada", "screwdriver", "singapore_sling", "tom_collins", "whiskey_sour", "white_russian"}; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setlistadapter(new arrayadapter<string>(menu.this,android.r.layout.simple_list_item_1, classes)); } @override protected void onlistitemclick(listview l, view v, int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, v, position, id);; intent ourintent = new intent(menu.this, cocktaildetail.class); ourintent.putextra("bloody_mary", "capirinha"); startactivity(ourintent); } }
code changes information of cocktail display page
package com.drunktxtapp; import android.net.uri; public class cocktaildetail extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cocktaildetail); imageview imageview1 = (imageview)findviewbyid(r.id.imagecocktail); imageview1.setimagedrawable(getresources().getdrawable(r.drawable.bloodymary)); button b1 = (button) findviewbyid(r.id.byoutube); textview t1 = (textview)findviewbyid(r.id.textcocktailname); string cocktailname = getintent().getstringextra("bloody mary"); t1.settext(cocktailname); b1.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { startactivity(new intent(intent.action_view, uri.parse("http://www.youtube.com/watch?v=alt-ehdc3fc"))); } }); } }
here logcat
04-18 09:22:38.085: e/androidruntime(1068): @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 04-18 09:22:38.085: e/androidruntime(1068): @ android.os.handler.dispatchmessage(handler.java:99) 04-18 09:22:38.085: e/androidruntime(1068): @ android.os.looper.loop(looper.java:137) 04-18 09:22:38.085: e/androidruntime(1068): @ android.app.activitythread.main(activitythread.java:4898) 04-18 09:22:38.085: e/androidruntime(1068): @ java.lang.reflect.method.invokenative(native method) 04-18 09:22:38.085: e/androidruntime(1068): @ java.lang.reflect.method.invoke(method.java:511) 04-18 09:22:38.085: e/androidruntime(1068): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 04-18 09:22:38.085: e/androidruntime(1068): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 04-18 09:22:38.085: e/androidruntime(1068): @ dalvik.system.nativestart.main(native method) 04-18 09:22:38.085: e/androidruntime(1068): caused by: java.lang.nullpointerexception 04-18 09:22:38.085: e/androidruntime(1068): @ com.drunktxtapp.cocktaildetail.oncreate(cocktaildetail.java:26) 04-18 09:22:38.085: e/androidruntime(1068): @ android.app.activity.performcreate(activity.java:5206) 04-18 09:22:38.085: e/androidruntime(1068): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1083) 04-18 09:22:38.085: e/androidruntime(1068): @ android.app.activitythread.performlaunchactivity(activitythread.java:2064) 04-18 09:22:38.085: e/androidruntime(1068): ... 11 more 04-18 09:27:46.270: i/process(1068): sending signal. pid: 1068 sig: 9
xml file
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/beer" android:id="@+id/cocktaildetail" > <textview android:id="@+id/textcocktailname" android:textstyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="cocktail name" android:textappearance="?android:attr/textappearancelarge" /> <imageview android:id="@+id/imagecocktail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/bloodymary" /> <textview android:textstyle="bold" android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="ingredients" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textingredients" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="insert txt here" android:textappearance="?android:attr/textappearancesmall" android:textstyle="bold" /> <textview android:textstyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margintop="2dp" android:text="preparation" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textprepration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="insert txt here" android:textappearance="?android:attr/textappearancesmall" android:textstyle="bold" /> <button android:id="@+id/buttonyoutube" android:layout_width="200dp" android:layout_height="75dp" android:layout_gravity="center" android:layout_margintop="5dp" android:text="youtube clip" android:textsize="20dp" /> </linearlayout>
i don't see button id "byoutube" in xml file. looks when button, call returns null, when try set onclicklistener, app crashes.
Comments
Post a Comment