java - Button not working. Keeps on forcing app close -
whenever insert code (button) fragment, app forced close. made xml file , call in fragment java has adapter , in xml use framelayout put button @ bottom of page. try using footer same error got. knows how solve one? :)
xml file
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/takequiz" android:orientation="horizontal" android:background="@drawable/bg_linux_commands" android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal"> <listview android:id="@id/android:list" android:layout_width="300dp" android:layout_height="300dp" android:layout_margintop="125dp"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:id="@+id/linearlayout" android:layout_height="50dp" android:gravity="center" android:layout_gravity="bottom"> <button android:id="@+id/buttontest" android:layout_width="fill_parent android:layout_height="wrap_content" android:gravity="center" android:text="@string/test" > </button> </linearlayout> </framelayout>
fragment java
public class linuxcommands extends listfragment{ string classes[] = { "file managing system", "scripting , text processing","process , system utility", "system information commands","archival , comparison commands","command shells"}; @suppresslint("inflateparams") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // actionbar actionbar mactionbar = getactivity().getactionbar(); mactionbar.setdisplayshowhomeenabled(true); mactionbar.setdisplayshowtitleenabled(false); layoutinflater minflater = layoutinflater.from(getactivity()); view mcustomview = minflater.inflate(r.layout.actionbar_ttg, null); textview mtextview = (textview) mcustomview.findviewbyid(r.id.actionbar_ttg); mtextview.settext("linux commands"); typeface custom_font = typeface.createfromasset(getactivity().getassets(), "fonts/brlnsr.ttf"); mtextview.settypeface(custom_font); mtextview.settextsize(18); mtextview.setgravity(gravity.center_vertical | gravity.center_horizontal); mactionbar.setcustomview(mcustomview); mactionbar.setdisplayshowcustomenabled(true); //button final button button = (button) getview().findviewbyid(r.id.buttontest); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // quiz intent intent = new intent (getactivity(), terminal.class); startactivity(intent); } }); listadapter adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, classes); // setting list adapter listfragment setlistadapter(adapter); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_linux_comm, container, false); } @override public void onlistitemclick(listview l, view v, int position, long id) { if(position == 0) { intent intent = new intent (getactivity(), filemanagingsystemlist.class); startactivity(intent); } else if (position == 1){ intent intent = new intent (getactivity(),scriptingandtextprocessinglist.class); startactivity(intent); } else if (position == 2){ intent intent = new intent (getactivity(),processandsystemutilitylist.class); startactivity(intent); } else if (position == 3){ intent intent = new intent (getactivity(),systeminformationcmdlist.class); startactivity(intent); } else if (position == 4){ intent intent = new intent (getactivity(),archivalandcompressioncmdlist.class); startactivity(intent); } else{ intent intent = new intent (getactivity(),commandshellslist.class); startactivity(intent); } } }
logcat
12-06 11:50:54.069: e/androidruntime(26672): fatal exception: main 12-06 11:50:54.069: e/androidruntime(26672): java.lang.nullpointerexception 12-06 11:50:54.069: e/androidruntime(26672): @ com.example.ttg.fragment.linuxcommands.oncreate(linuxcommands.java:56) 12-06 11:50:54.069: e/androidruntime(26672): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.app.backstackrecord.run(backstackrecord.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.app.fragmentmanagerimpl$1.run(fragmentmanager.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.os.handler.handlecallback(handler.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.os.handler.dispatchmessage(handler.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.os.looper.loop(looper.java) 12-06 11:50:54.069: e/androidruntime(26672): @ android.app.activitythread.main(activitythread.java) 12-06 11:50:54.069: e/androidruntime(26672): @ java.lang.reflect.method.invokenative(native method) 12-06 11:50:54.069: e/androidruntime(26672): @ java.lang.reflect.method.invoke(method.java:511) 12-06 11:50:54.069: e/androidruntime(26672): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java) 12-06 11:50:54.069: e/androidruntime(26672): @ com.android.internal.os.zygoteinit.main(zygoteinit.java) 12-06 11:50:54.069: e/androidruntime(26672): @ dalvik.system.nativestart.main(native method)
in fragment should use oncreateview()
method.
@override public view oncreateview(layoutinflater inflater,viewgroup container, bundle args) { root = inflater.inflate(r.layout.schedule_fragment, container, false); startbutton = (button)root.findviewbyid(r.id.button); startbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // code. } }); return root; }
Comments
Post a Comment