java - Custom ActionBar NullPointerException -
sorry english. have tabactivity , want create custom action bar, have error:
04-25 20:11:17.777: e/androidruntime(8083): java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.mainactivity}: java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.news}: java.lang.nullpointerexception
mainactivity:
public class mainactivity extends tabactivity { tabhost tabhost; progressdialog pdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tabhost = gettabhost(); settabs(); } private void settabs() { addtab("", r.drawable.tab_news, news.class); addtab("", r.drawable.tab_servises, news.class); addtab("", r.drawable.tab_profile, news.class); addtab("", r.drawable.tab_contacts, news.class); } private void addtab(string labelid, int drawableid, class<?> c) { intent intent = new intent(this, c); tabhost.tabspec spec = tabhost.newtabspec("tab" + labelid); view tabindicator = layoutinflater.from(this).inflate(r.layout.tab_indicator, gettabwidget(), false); textview title = (textview) tabindicator.findviewbyid(r.id.title); title.settext(labelid); imageview icon = (imageview) tabindicator.findviewbyid(r.id.icon); icon.setimageresource(drawableid); spec.setindicator(tabindicator); spec.setcontent(intent); tabhost.addtab(spec); } }
news
public class news extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_n); actionbar mactionbar = getactionbar(); mactionbar.setdisplayshowhomeenabled(false); mactionbar.setdisplayshowtitleenabled(false); layoutinflater minflater = layoutinflater.from(this); view mcustomview = minflater.inflate(r.layout.custom_actionbar, null); mactionbar.setcustomview(mcustomview); mactionbar.setdisplayshowcustomenabled(true); } }
upd
all log cat
04-25 20:33:15.186: e/androidruntime(12242): fatal exception: main 04-25 20:33:15.186: e/androidruntime(12242): java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.mainactivity}: java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.news}: java.lang.nullpointerexception 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.performlaunchactivity(activitythread.java:2077) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2104) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.access$600(activitythread.java:134) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread$h.handlemessage(activitythread.java:1247) 04-25 20:33:15.186: e/androidruntime(12242): @ android.os.handler.dispatchmessage(handler.java:99) 04-25 20:33:15.186: e/androidruntime(12242): @ android.os.looper.loop(looper.java:154) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.main(activitythread.java:4624) 04-25 20:33:15.186: e/androidruntime(12242): @ java.lang.reflect.method.invokenative(native method) 04-25 20:33:15.186: e/androidruntime(12242): @ java.lang.reflect.method.invoke(method.java:511) 04-25 20:33:15.186: e/androidruntime(12242): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:809) 04-25 20:33:15.186: e/androidruntime(12242): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:576) 04-25 20:33:15.186: e/androidruntime(12242): @ dalvik.system.nativestart.main(native method) 04-25 20:33:15.186: e/androidruntime(12242): caused by: java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.news}: java.lang.nullpointerexception 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.performlaunchactivity(activitythread.java:2077) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.startactivitynow(activitythread.java:1917) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.localactivitymanager.movetostate(localactivitymanager.java:135) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.localactivitymanager.startactivity(localactivitymanager.java:347) 04-25 20:33:15.186: e/androidruntime(12242): @ android.widget.tabhost$intentcontentstrategy.getcontentview(tabhost.java:682) 04-25 20:33:15.186: e/androidruntime(12242): @ android.widget.tabhost.setcurrenttab(tabhost.java:346) 04-25 20:33:15.186: e/androidruntime(12242): @ android.widget.tabhost.addtab(tabhost.java:236) 04-25 20:33:15.186: e/androidruntime(12242): @ com.example.bonsitelazyl.mainactivity.addtab(mainactivity.java:47) 04-25 20:33:15.186: e/androidruntime(12242): @ com.example.bonsitelazyl.mainactivity.settabs(mainactivity.java:29) 04-25 20:33:15.186: e/androidruntime(12242): @ com.example.bonsitelazyl.mainactivity.oncreate(mainactivity.java:24) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activity.performcreate(activity.java:4479) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1050) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.performlaunchactivity(activitythread.java:2041) 04-25 20:33:15.186: e/androidruntime(12242): ... 11 more 04-25 20:33:15.186: e/androidruntime(12242): caused by: java.lang.nullpointerexception 04-25 20:33:15.186: e/androidruntime(12242): @ com.example.bonsitelazyl.news.oncreate(news.java:15) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activity.performcreate(activity.java:4479) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1050) 04-25 20:33:15.186: e/androidruntime(12242): @ android.app.activitythread.performlaunchactivity(activitythread.java:2041)
upd:
getactionbar return null
upd:
i try android.support.v7.app.actionbaractivity in new update, add code:
getsupportactionbar().setdisplayshowcustomenabled(true); view customnav = layoutinflater.from(this).inflate(r.layout.custom_actionbar, null); getsupportactionbar().setcustomview(customnav); getsupportactionbar().setdisplayshowtitleenabled(false); getsupportactionbar().setcustomview(customnav);
04-25 21:09:09.550: e/androidruntime(22075): java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.mainactivity}: java.lang.runtimeexception: unable start activity componentinfo{com.example.bonsitelazyl/com.example.bonsitelazyl.news}: java.lang.nullpointerexception
the tabactivity deprecated class. add tabs class use code above.
mainactivity2activity.java
import java.util.locale; import android.support.v7.app.actionbaractivity; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.os.bundle; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; public class mainactivity2activity extends actionbaractivity { sectionspageradapter msectionspageradapter; viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_activity2); msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main_activity2, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return placeholderfragment.newinstance(position + 1); } @override public int getcount() { return 3; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.title_section1).touppercase(l); case 1: return getstring(r.string.title_section2).touppercase(l); case 2: return getstring(r.string.title_section3).touppercase(l); } return null; } } public static class placeholderfragment extends fragment { private static final string arg_section_number = "section_number"; public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main_activity2, container, false); return rootview; } } }
layout/activity_main_activity2.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="georgeoikonomou.fishinggps.mainactivity2activity"/>
layout/fragment_main_activity2.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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="georgeoikonomou.fishinggps.mainactivity2activity$placeholderfragment"> <textview android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </relativelayout>
Comments
Post a Comment