Monday, 15 July 2013

java - Android Developing App crashes -



java - Android Developing App crashes -

hi guys, started larn developing app android today. guide new boston: https://buckysroom.org/videos.php?cat=6&video=16731 passed every tutorial until 10th because: did every step, app crashes. here xml code:

<button android:id="@+id/badd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvdisplay" android:layout_centerhorizontal="true" android:layout_margintop="50dp" android:text="add here" /> <textview android:id="@+id/tvdisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="18dp" android:text="the total is: " /> </relativelayout>

and here java code:

package com.example.firstpro; public class mainactivity extends actionbaractivity { int c = 0; button add, sub; textview display; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); add together = (button) findviewbyid(r.id.badd); display = (textview) findviewbyid(r.id.tvdisplay); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub c+=1; display.settext("you're total is: " + c); setcontentview(r.layout.activity_main); } }); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); homecoming rootview; } } }

i don't know what's problem here. there no error warning. hope answers. thanks.

your button , textview in fragment layout, not in activity layout have move code oncreateview:

public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } int c=0; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); button add together = (button) rootview.findviewbyid(r.id.badd); textview display = (textview) rootview.findviewbyid(r.id.tvdisplay); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub c+=1; display.settext("you're total is: " + c); //you don't need set same content view 1 time again //setcontentview(r.layout.activity_main); } }); homecoming rootview; } } }

java android eclipse

No comments:

Post a Comment