android - HorizontalScrollabe with TabHost gives error -
i have 14 tabs in activity, hence have used horizontalscrollable
make scrollable. gives nullpointerexception
error. cannot figure out why.
the xml:
<tabhost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <horizontalscrollview android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillviewport="true" android:scrollbars="none"> <tabwidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" > </tabwidget> </horizontalscrollview> <framelayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <relativelayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab4" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab5" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab6" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab7" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> </relativelayout> </framelayout> </linearlayout> </tabhost>
here code:
public class events2 extends activity { private tabhost thevent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_events2); thevent = (tabhost) findviewbyid(r.id.tabhost); thevent.setup(); // tab 1 tabspec specs = thevent.newtabspec("tag1"); specs.setcontent(r.id.tab1); specs.setindicator("petrogyan"); thevent.addtab(specs); specs = thevent.newtabspec("tag2"); specs.setcontent(r.id.tab2); specs.setindicator("petrotalk"); thevent.addtab(specs); specs = thevent.newtabspec("tag3"); specs.setcontent(r.id.tab3); specs.setindicator("petro case study"); thevent.addtab(specs); specs = thevent.newtabspec("tag4"); specs.setcontent(r.id.tab4); specs.setindicator("petrodraft"); thevent.addtab(specs); specs = thevent.newtabspec("tag5"); specs.setcontent(r.id.tab5); specs.setindicator("petrosell"); thevent.addtab(specs); specs = thevent.newtabspec("tag6"); specs.setcontent(r.id.tab6); specs.setindicator("petro-debate"); thevent.addtab(specs); specs = thevent.newtabspec("tag7"); specs.setcontent(r.id.tab7); specs.setindicator("idp"); thevent.addtab(specs); } }
p.s. have shortened code post this. using 14 tabs in original activity
logcat:
09-20 19:36:35.461: e/androidruntime(2796): fatal exception: main 09-20 19:36:35.461: e/androidruntime(2796): java.lang.runtimeexception: unable start activity componentinfo{com.rakeshsarangi.petrofiesta2013/com.rakeshsarangi.petrofiesta2013.events2}: java.lang.nullpointerexception 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activitythread.performlaunchactivity(activitythread.java:1955) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1980) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activitythread.access$600(activitythread.java:122) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activitythread$h.handlemessage(activitythread.java:1146) 09-20 19:36:35.461: e/androidruntime(2796): @ android.os.handler.dispatchmessage(handler.java:99) 09-20 19:36:35.461: e/androidruntime(2796): @ android.os.looper.loop(looper.java:137) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activitythread.main(activitythread.java:4340) 09-20 19:36:35.461: e/androidruntime(2796): @ java.lang.reflect.method.invokenative(native method) 09-20 19:36:35.461: e/androidruntime(2796): @ java.lang.reflect.method.invoke(method.java:511) 09-20 19:36:35.461: e/androidruntime(2796): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 09-20 19:36:35.461: e/androidruntime(2796): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 09-20 19:36:35.461: e/androidruntime(2796): @ dalvik.system.nativestart.main(native method) 09-20 19:36:35.461: e/androidruntime(2796): caused by: java.lang.nullpointerexception 09-20 19:36:35.461: e/androidruntime(2796): @ com.rakeshsarangi.petrofiesta2013.events2.oncreate(events2.java:22) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activity.performcreate(activity.java:4465) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 09-20 19:36:35.461: e/androidruntime(2796): @ android.app.activitythread.performlaunchactivity(activitythread.java:1919) 09-20 19:36:35.461: e/androidruntime(2796): ... 11 more
// try 1. when u define id xml way android:id="@android:id/tabhost" u in activity findviewbyid(android.r.id.tabhost); 2. when u define id xml way android:id="@+id/tab1" u in activity findviewbyid(r.id.tab1); // in ur case try <?xml version="1.0" encoding="utf-8"?> <tabhost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <horizontalscrollview android:layout_width="match_parent" android:layout_height="wrap_content" android:fillviewport="true" android:scrollbars="none"> <tabwidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" > </tabwidget> </horizontalscrollview> <framelayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <relativelayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab4" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab5" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab6" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <relativelayout android:id="@+id/tab7" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> </framelayout> </linearlayout> </tabhost> private tabhost thevent; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); thevent = (tabhost) findviewbyid(android.r.id.tabhost); thevent.setup(); // tab 1 tabhost.tabspec specs = thevent.newtabspec("tag1"); specs.setcontent(r.id.tab1); specs.setindicator("petrogyan"); thevent.addtab(specs); specs = thevent.newtabspec("tag2"); specs.setcontent(r.id.tab2); specs.setindicator("petrotalk"); thevent.addtab(specs); specs = thevent.newtabspec("tag3"); specs.setcontent(r.id.tab3); specs.setindicator("petro case study"); thevent.addtab(specs); specs = thevent.newtabspec("tag4"); specs.setcontent(r.id.tab4); specs.setindicator("petrodraft"); thevent.addtab(specs); specs = thevent.newtabspec("tag5"); specs.setcontent(r.id.tab5); specs.setindicator("petrosell"); thevent.addtab(specs); specs = thevent.newtabspec("tag6"); specs.setcontent(r.id.tab6); specs.setindicator("petro-debate"); thevent.addtab(specs); specs = thevent.newtabspec("tag7"); specs.setcontent(r.id.tab7); specs.setindicator("idp"); thevent.addtab(specs); }
Comments
Post a Comment