android - Implementing swipe tabs with action bar giving me an exception -
i trying implement tabs action bar in activity. getting exception. reason? code:
mainactivity.java
public class mainactivity extends fragmentactivity implements actionbar.tablistener { sectionspageradapter msectionspageradapter; viewpager mviewpager; string arr[]=new string[]{"vegetables","drinks","fruits","chai","eatables"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); mviewpager .setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); (int = 0; < msectionspageradapter.getcount(); i++) { // create tab text corresponding page title defined // adapter. specify activity object, implements // tablistener interface, callback (listener) when // tab selected. actionbar.addtab(actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return 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(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return dummysectionfragment (defined static inner class // below) page number lone argument. string str[]=null; for(int i=0;i<10;i++) str[i]=arr[position]; fragment fragment = new homefragment(); bundle bundle =new bundle(); bundle.putstringarray("air",str); fragment.setarguments(bundle); return fragment; } @override public int getcount() { // show 3 total pages. return arr.length; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); return arr[position]; } } }
this exception getting:
08-05 16:12:44.439 10012-10012/com.works.vipul.peppertap3 e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.works.vipul.peppertap3/com.works.vipul.peppertap3.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:1970) @ android.app.activitythread.handlelaunchactivity(activitythread.java:1995) @ android.app.activitythread.access$600(activitythread.java:128) @ android.app.activitythread$h.handlemessage(activitythread.java:1161) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4517) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:760) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.works.vipul.peppertap3.mainactivity.oncreate(mainactivity.java:31) @ android.app.activity.performcreate(activity.java:4470) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1053) @ android.app.activitythread.performlaunchactivity(activitythread.java:1934) at android.app.activitythread.handlelaunchactivity(activitythread.java:1995) at android.app.activitythread.access$600(activitythread.java:128) at android.app.activitythread$h.handlemessage(activitythread.java:1161) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:4517) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) at com.android.internal.os.zygoteinit.main(zygoteinit.java:760) at dalvik.system.nativestart.main(native method) 08-05 16:12:54.729 10012-10012/com.works.vipul.peppertap3 i/process﹕ sending signal. pid: 10012 sig: 9 08-05 16:14:16.039 10483-10483/com.works.vipul.peppertap3 d/androidruntime﹕ shutting down vm 08-05 16:14:16.039 10483-10483/com.works.vipul.peppertap3 w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x40c1aa68) 08-05 16:14:16.059 10483-10483/com.works.vipul.peppertap3 e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.works.vipul.peppertap3/com.works.vipul.peppertap3.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:1970) @ android.app.activitythread.handlelaunchactivity(activitythread.java:1995) @ android.app.activitythread.access$600(activitythread.java:128) @ android.app.activitythread$h.handlemessage(activitythread.java:1161) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4517) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:760) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.works.vipul.peppertap3.mainactivity.oncreate(mainactivity.java:31) @ android.app.activity.performcreate(activity.java:4470) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1053) @ android.app.activitythread.performlaunchactivity(activitythread.java:1934) at android.app.activitythread.handlelaunchactivity(activitythread.java:1995) at android.app.activitythread.access$600(activitythread.java:128) at android.app.activitythread$h.handlemessage(activitythread.java:1161) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:4517) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) at com.android.internal.os.zygoteinit.main(zygoteinit.java:760) at dalvik.system.nativestart.main(native method) 08-05 16:14:28.759 10483-10483/com.works.vipul.peppertap3 i/process﹕ sending signal. pid: 10483 sig: 9 08-05 16:15:31.819 10911-10911/com.works.vipul.peppertap3 d/androidruntime﹕ shutting down vm 08-05 16:15:31.819 10911-10911/com.works.vipul.peppertap3 w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x40c1aa68) 08-05 16:15:31.839 10911-10911/com.works.vipul.peppertap3 e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.works.vipul.peppertap3/com.works.vipul.peppertap3.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:1970) @ android.app.activitythread.handlelaunchactivity(activitythread.java:1995) @ android.app.activitythread.access$600(activitythread.java:128) @ android.app.activitythread$h.handlemessage(activitythread.java:1161) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4517) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:760) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.works.vipul.peppertap3.mainactivity.oncreate(mainactivity.java:31) @ android.app.activity.performcreate(activity.java:4470) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1053) @ android.app.activitythread.performlaunchactivity(activitythread.java:1934) at android.app.activitythread.handlelaunchactivity(activitythread.java:1995) at android.app.activitythread.access$600(activitythread.java:128) at android.app.activitythread$h.handlemessage(activitythread.java:1161) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:4517) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) at com.android.internal.os.zygoteinit.main(zygoteinit.java:760) at dalvik.system.nativestart.main(native method) 08-05 16:15:41.659 10911-10911/com.works.vipul.peppertap3 i/process﹕ sending signal. pid: 10911 sig: 9
this fragment code:
homefragment.java
package com.works.vipul.peppertap3; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.listview; public class homefragment extends fragment { public homefragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main_dummy, container, false); string arr[]= getarguments().getstringarray("air"); listview l=(listview)rootview.findviewbyid(r.id.section_label); arrayadapter a=new arrayadapter<string>(getactivity(),android.r.layout.simple_dropdown_item_1line,arr); l.setadapter(a); return rootview; } }
these layouts:
activity_main.xml
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" />
fragment_main_dummy.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: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$dummysectionfragment" > <listview android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:textsize="25sp" /> </relativelayout>
try use :
getsupportactionbar()
instead of:
getactionbar().
you that:
import android.support.v7.app.actionbar; import android.support.v7.app.appcompatactivity; public class entryactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs);
Comments
Post a Comment