android - Switch tabs from fragment -
i using 2 java files creating tab layout slidingtablayout.java & slidingtabstrip.java , hav fragments tabs. have button in tab1 switch tab2 doesnt work . using below code button in tab1
tabhost host = (tabhost) getactivity().findviewbyid( android.r.id.tabhost); host.setcurrenttab(2);
the above code throws null pointer please help
this tablayout file
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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:orientation="vertical" tools:context=".mainactivity" > <include android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/tool_bar" /> <com.datavsn.adapter.slidingtablayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorprimary" android:elevation="2dp" /> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > </android.support.v4.view.viewpager> </linearlayout>
this error like
08-31 14:10:17.948: e/androidruntime(5961): fatal exception: main 08-31 14:10:17.948: e/androidruntime(5961): process: com.datavsn.microfinance, pid: 5961 08-31 14:10:17.948: e/androidruntime(5961): java.lang.nullpointerexception 08-31 14:10:17.948: e/androidruntime(5961): @ com.datavsn.microfinance.loandisbursement.inittabhost(loandisbursement.java:102) 08-31 14:10:17.948: e/androidruntime(5961): @ com.datavsn.microfinance.loandisbursement.onactivitycreated(loandisbursement.java:57) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.fragment.performactivitycreated(fragment.java:1797) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:979) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1138) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:740) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1501) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:490) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.view.viewpager.populate(viewpager.java:1105) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.view.viewpager.populate(viewpager.java:951) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1473) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1410) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.measurevertical(linearlayout.java:695) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.framelayout.onmeasure(framelayout.java:310) 08-31 14:10:17.948: e/androidruntime(5961): @ android.support.v7.internal.widget.contentframelayout.onmeasure(contentframelayout.java:124) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1410) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.measurevertical(linearlayout.java:695) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.framelayout.onmeasure(framelayout.java:310) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1410) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.measurevertical(linearlayout.java:695) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 08-31 14:10:17.948: e/androidruntime(5961): @ android.widget.framelayout.onmeasure(framelayout.java:310) 08-31 14:10:17.948: e/androidruntime(5961): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2548) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.view.measure(view.java:17478) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2285) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1396) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1595) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1254) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6624) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.choreographer$callbackrecord.run(choreographer.java:812) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.choreographer.docallbacks(choreographer.java:612) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.choreographer.doframe(choreographer.java:582) 08-31 14:10:17.948: e/androidruntime(5961): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:798) 08-31 14:10:17.948: e/androidruntime(5961): @ android.os.handler.handlecallback(handler.java:733) 08-31 14:10:17.948: e/androidruntime(5961): @ android.os.handler.dispatchmessage(handler.java:95) 08-31 14:10:17.948: e/androidruntime(5961): @ android.os.looper.loop(looper.java:146) 08-31 14:10:17.948: e/androidruntime(5961): @ android.app.activitythread.main(activitythread.java:5602) 08-31 14:10:17.948: e/androidruntime(5961): @ java.lang.reflect.method.invokenative(native method) 08-31 14:10:17.948: e/androidruntime(5961): @ java.lang.reflect.method.invoke(method.java:515) 08-31 14:10:17.948: e/androidruntime(5961): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) 08-31 14:10:17.948: e/androidruntime(5961): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) 08-31 14:10:17.948: e/androidruntime(5961): @ dalvik.system.nativestart.main(native method)
my java code first tab fragment
import java.util.arraylist; import java.util.list; import com.datavsn.adapter.tabspageradapter; import com.datavsn.database.dbhelper; import com.datavsn.database.databasehelper; import android.r.string; import android.app.activity; import android.app.alertdialog; import android.app.tabactivity; import android.content.context; import android.content.dialoginterface; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.webkit.webview.findlistener; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.spinner; import android.widget.adapterview.onitemselectedlistener; import android.widget.tabhost; /** * @since ver 1.0 modified akshay on 26 aug 2015 * <li>changes made make activity fragment</li> */ public class loandisbursement extends fragment { dbhelper dbhelper; edittext mode, batchno, meetinno,institutionno; spinner spinneragainstmeeting, spinnertrans,institutiontype; button btnchoose,btnsave; viewpager pager; tabspageradapter move; arraylist<string> institutionname = new arraylist<string>(); @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_loan, container, false); return rootview; } @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); // context context = getactivity().getapplicationcontext(); initviews(); institutiontype.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub institutionname = dbhelper.getinstitutionnamebygroupid(dbhelper .getgroupid(institutiontype.getselecteditem() .tostring())); if (institutionname.size() > 0) { institutionno.settext(institutionname.get(0)); } else { institutionno.settext(""); } } @override public void onnothingselected(adapterview<?> parent) { // todo auto-generated method stub } }); btnchoose.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub loadspinnerinsno(); } }); btnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub tabhost host = (tabhost) getactivity().findviewbyid( android.r.id.tabhost); host.setcurrenttab(2); } }); } private void initviews() { // todo auto-generated method stub dbhelper = new dbhelper(getactivity()); mode = (edittext) getview().findviewbyid(r.id.edtmode); batchno = (edittext) getview().findviewbyid(r.id.edtbatch); institutiontype = (spinner) getview().findviewbyid( r.id.spninstitutintype); institutionno = (edittext) getview() .findviewbyid(r.id.edtinstitutionno); meetinno = (edittext) getview().findviewbyid(r.id.edtmeetingno); spinneragainstmeeting = (spinner) getview().findviewbyid( r.id.spinneragainestmeet); spinnertrans = (spinner) getview().findviewbyid(r.id.spinnertrans); btnchoose = (button) getview().findviewbyid(r.id.btnchoose); btnsave = (button) getview().findviewbyid(r.id.btnsave); loadspinnerinstype(); loadspinneragainstmeet(); mode.settext("cash"); mode.setenabled(false); batchno.requestfocus(); move = new tabspageradapter(getfragmentmanager()); } /* * @since version 1.0.0 added aniket w.r.t.#12092 on 25 aug 2015 * method used yes or no spinner */ private void loadspinneragainstmeet() { // todo auto-generated method stub arraylist<string> againstmeet = new arraylist<string>(); againstmeet.add("yes"); againstmeet.add("no"); // creating adapter spinner arrayadapter<string> dataadapter = new arrayadapter<string>( getactivity(), android.r.layout.simple_spinner_item, againstmeet); dataadapter .setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching data adapter spinner spinneragainstmeeting.setadapter(dataadapter); } private void loadspinnerinstype() { // todo auto-generated method stub arraylist<string> labels = dbhelper.getgroupnames(); // creating adapter spinner arrayadapter<string> dataadapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_spinner_item, labels); // drop down layout style - list view radio button dataadapter .setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching data adapter spinner institutiontype.setadapter(dataadapter); } private void loadspinnerinsno() { // todo auto-generated method stub list<string> labels = dbhelper.getinstitutionnamebygroupid(dbhelper .getgroupid(institutiontype.getselecteditem().tostring())); alertdialog.builder alert = new alertdialog.builder(getactivity()); final arrayadapter<string> dataadapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, labels); alert.setadapter(dataadapter, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { string strname = dataadapter.getitem(which); alertdialog.builder builderinner = new alertdialog.builder( getactivity()); builderinner.setmessage(strname); institutionno.settext(strname); } }); alert.show(); } }
you have 2 tab, set
host.setcurrenttab(1);
actually starts 0 ,
tabhost host = (tabhost) getactivity().findviewbyid(android.r.id.tabhost); host.setcurrenttab(1);
for more details can visit:
https://developer.android.com/reference/android/widget/tabhost.html#setcurrenttab(int)
edited answer
use getview() instead getactivity()
. hope works .
tabhost host = (tabhost) getview().findviewbyid(android.r.id.tabhost);
Comments
Post a Comment