listview - Android: listview1.setAdapter null exception -
my app crashes @ line: listview1.setadapter(new arrayadapter(mainactivity.getappcontext(), android.r.layout.simple_list_item_1, listcontents));
my code:
package de.nathan.android.droidschool; import android.content.context; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import java.util.arraylist; import java.util.list; import java.util.locale; public class mainactivity extends fragmentactivity { private static context context; /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link android.support.v4.app.fragmentpageradapter} derivative, * keep every loaded fragment in memory. if becomes memory * intensive, may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create adapter return fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); mainactivity.context = getapplicationcontext(); } public static context getappcontext() { return mainactivity.context; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ 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. fragment fragment; switch(position) { case 0: fragment = new fragmenttab1(); break; case 1: fragment = new fragmenttab2(); break; case 2: fragment = new fragmenttab3(); break; default: throw new illegalargumentexception("invalid section number"); } bundle args = new bundle(); args.putint(fragmenttab1.arg_section_number, position + 1); fragment.setarguments(args); return fragment; } @override public int getcount() { // show 3 total pages. 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; } } /** * dummy fragment representing section of app, * displays dummy text. */ public class fragmenttab1 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public void onstart() { list listcontents = new arraylist(); listcontents.add("one"); listcontents.add("two"); listcontents.add("three"); listcontents.add("four"); listcontents.add("five"); listcontents.add("six"); listcontents.add("seven"); listcontents.add("eight"); listcontents.add("nine"); listcontents.add("ten"); listcontents.add("eleven"); listview listview1; listview1 = (listview) findviewbyid(r.id.listview); listview1.setadapter(new arrayadapter(mainactivity.getappcontext(), android.r.layout.simple_list_item_1, listcontents)); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_layout_tab1, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public class fragmenttab2 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragmenttab2() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_layout_tab2, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public class fragmenttab3 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragmenttab3() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_layout_tab3, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } }
and here logcat:
08-12 23:51:56.885 6008-6008/de.nathan.android.droidschool i/system.out: debugger has settled (1347) 08-12 23:51:56.975 6088-6092/? d/dalvikvm: gc_concurrent freed 398k, 6% free 7821k/8280k, paused 7ms+2ms, total 62ms 08-12 23:51:56.975 6088-6088/? d/dalvikvm: wait_for_concurrent_gc blocked 22ms 08-12 23:51:56.995 25995-26080/? d/dalvikvm: gc_for_alloc freed 71k, 7% free 8744k/9316k, paused 73ms, total 74ms 08-12 23:51:57.005 25995-6084/? i/gcoreulr: ensuring reporting stopped because of reasons: {account#-13#=[inactivereason{mversioncode=0, midentifier=6, mname='reportingnotselected'}]} 08-12 23:51:57.005 25995-6084/? d/gcoreflp: unknown pending intent remove. 08-12 23:51:57.015 25995-6084/? i/gcoreulr: unbound location providers 08-12 23:51:57.085 6088-6092/? d/dalvikvm: gc_concurrent freed 497k, 7% free 7838k/8396k, paused 2ms+4ms, total 38ms 08-12 23:51:57.085 6088-6103/? d/dalvikvm: wait_for_concurrent_gc blocked 11ms 08-12 23:51:57.085 6088-6088/? d/dalvikvm: wait_for_concurrent_gc blocked 16ms 08-12 23:51:57.095 25995-26080/? d/dalvikvm: gc_for_alloc freed 38k, 7% free 8738k/9316k, paused 47ms, total 47ms 08-12 23:51:57.155 438-900/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.155 438-900/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.195 438-6553/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.195 438-6553/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.225 438-6553/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.225 438-6553/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.235 6088-6092/? d/dalvikvm: gc_concurrent freed 440k, 6% free 7911k/8412k, paused 4ms+4ms, total 52ms 08-12 23:51:57.235 6088-6088/? d/dalvikvm: wait_for_concurrent_gc blocked 36ms 08-12 23:51:57.245 438-831/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.245 438-831/? w/activitymanager: permission denied: checkcomponentpermission() owninguid=10055 08-12 23:51:57.275 6008-6008/de.nathan.android.droidschool d/androidruntime: shutting down vm 08-12 23:51:57.275 6008-6008/de.nathan.android.droidschool w/dalvikvm: threadid=1: thread exiting uncaught exception (group=0x41760700) 08-12 23:51:57.295 6088-6088/? i/d: channel: 08-12 23:51:57.305 6063-6067/? d/dalvikvm: gc_concurrent freed 379k, 6% free 7749k/8192k, paused 16ms+4ms, total 41ms 08-12 23:51:57.305 6008-6008/de.nathan.android.droidschool e/androidruntime: fatal exception: main android.support.v4.app.supernotcalledexception: fragment fragmenttab1{41ef7c58 #0 id=0x7f080000 android:switcher:2131230720:0} did not call through super.onstart() @ android.support.v4.app.fragment.performstart(fragment.java:1486) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:941) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1088) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1444) @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:461) @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) @ android.support.v4.view.viewpager.populate(viewpager.java:1064) @ android.support.v4.view.viewpager.populate(viewpager.java:911) @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1432) @ android.view.view.measure(view.java:15848) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5008) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ android.view.view.measure(view.java:15848) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5008) @ com.android.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:302) @ android.view.view.measure(view.java:15848) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5008) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2189) @ android.view.view.measure(view.java:15848) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1905) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1104) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1284) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1004) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5481) @ android.view.choreographer$callbackrecord.run(choreographer.java:749) @ android.view.choreographer.docallbacks(choreographer.java:562) @ android.view.choreographer.doframe(choreographer.java:532) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) @ android.os.handler.handlecallback(handler.java:730) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5103) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method) 08-12 23:51:57.335 6088-6092/? d/dalvikvm: gc_concurrent freed 514k, 7% free 7909k/8484k, paused 3ms+3ms, total 55ms 08-12 23:51:57.335 6088-6088/? d/dalvikvm: wait_for_concurrent_gc blocked 34ms 08-12 23:51:57.335 438-901/? w/activitymanager: force finishing activity de.nathan.android.droidschool/.mainactivity 08-12 23:51:57.355 6088-6088/? i/licensechecker: binding licensing service. 08-12 23:51:57.635 438-901/? i/windowmanager: screenshot max retries 4 of token{42a105d8 activityrecord{422e4c88 u0 de.nathan.android.droidschool/.mainactivity}} appwin=window{41f107b8 u0 starting de.nathan.android.droidschool} drawstate=4 08-12 23:51:57.635 438-901/? w/windowmanager: screenshot failure taking screenshot (800x1280) layer 21010 08-12 23:51:57.645 438-817/? w/activitymanager: permission denial: accessing service componentinfo{com.android.vending/com.google.android.finsky.services.licensingservice} pid=6088, uid=10120 requires com.android.vending.check_license 08-12 23:51:57.655 6088-6088/? i/appupgradereceiver: notified of app replace. package:de.nathan.android.droidschool#intent;action=android.intent.action.package_replaced;launchflags=0x8000010;component=com.shazam.android/com.shazam.upgrade.appupgradereceiver;b.android.intent.extra.replacing=true;i.android.intent.extra.uid=10018;i.android.intent.extra.user_handle=0;end 08-12 23:51:57.655 438-664/? i/activitymanager: no longer want com.android.keychain (pid 5914): empty #17 08-12 23:51:57.745 6088-6092/? d/dalvikvm: gc_concurrent freed 253k, 5% free 8064k/8484k, paused 3ms+2ms, total 30ms 08-12 23:51:58.055 438-3894/? d/dalvikvm: gc_explicit freed 2659k, 24% free 19511k/25460k, paused 4ms+19ms, total 193ms 08-12 23:51:58.135 438-454/? w/activitymanager: activity pause timeout activityrecord{422e4c88 u0 de.nathan.android.droidschool/.mainactivity} 08-12 23:51:58.195 438-665/? i/activitymanager: no longer want jp.co.omronsoft.iwnnime.ml (pid 5931): empty #17 08-12 23:51:58.295 706-706/? i/androidinput: sensor listener setup 08-12 23:51:58.305 706-884/? i/androidgraphics: framebuffer: (5, 6, 5, 0) 08-12 23:51:58.305 706-884/? i/androidgraphics: depthbuffer: (16) 08-12 23:51:58.305 706-884/? i/androidgraphics: stencilbuffer: (0) 08-12 23:51:58.305 706-884/? i/androidgraphics: samples: (0) 08-12 23:51:58.305 706-884/? i/androidgraphics: coverage sampling: (false) 08-12 23:51:58.315 706-884/? i/androidgraphicslw: managed meshes/app: { } 08-12 23:51:58.315 706-884/? i/androidgraphicslw: managed textures/app: { } 08-12 23:51:58.315 706-884/? i/androidgraphicslw: managed shaders/app: { } 08-12 23:51:58.315 706-884/? i/androidgraphicslw: managed buffers/app: { } 08-12 23:51:58.315 706-884/? v/worldsize: landscape: false 08-12 23:52:00.505 16244-16244/? d/wetter.com: [23:52:00.508, main]: widget on receive: com.wetter.androidclient.widgets.wetterwidgetprovider4x2 08-12 23:52:00.515 16244-16244/? d/wetter.com: [23:52:00.514, main]: wetterwidgetupdateservice - onstartcommand 08-12 23:52:00.515 16244-16244/? d/wetter.com: [23:52:00.517, main]: widgetid: 33 - onstartcommand 08-12 23:52:00.515 16244-16244/? d/wetter.com: [23:52:00.518, main]: widgetid: 33 - onstartcommand - update datetime 08-12 23:52:00.625 16244-16244/? d/wetter.com: [23:52:00.634, main]: widgetid: 33 - set update timer clock 12.08.2013 23:53:00, pi: 2000033 08-12 23:52:02.235 15389-15393/? d/dalvikvm: gc_concurrent freed 339k, 7% free 8566k/9168k, paused 6ms+4ms, total 59ms 08-12 23:52:08.205 438-454/? w/activitymanager: activity destroy timeout activityrecord{422e4c88 u0 de.nathan.android.droidschool/.mainactivity}
logcat when switch landscape:
08-13 00:37:52.745 13523-13523/de.nathan.android.droidschool d/dalvikvm: newinstance failed: no <init>() 08-13 00:37:52.755 13523-13523/de.nathan.android.droidschool d/androidruntime: shutting down vm 08-13 00:37:52.755 13523-13523/de.nathan.android.droidschool w/dalvikvm: threadid=1: thread exiting uncaught exception (group=0x41760700) 08-13 00:37:52.815 13523-13523/de.nathan.android.droidschool e/androidruntime: fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{de.nathan.android.droidschool/de.nathan.android.droidschool.mainactivity}: android.support.v4.app.fragment$instantiationexception: unable instantiate fragment de.nathan.android.droidschool.mainactivity$fragmenttab1: make sure class name exists, public, , has empty constructor public @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3740) @ android.app.activitythread.access$700(activitythread.java:141) @ android.app.activitythread$h.handlemessage(activitythread.java:1262) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5103) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method) caused by: android.support.v4.app.fragment$instantiationexception: unable instantiate fragment de.nathan.android.droidschool.mainactivity$fragmenttab1: make sure class name exists, public, , has empty constructor public @ android.support.v4.app.fragment.instantiate(fragment.java:405) @ android.support.v4.app.fragmentstate.instantiate(fragment.java:97) @ android.support.v4.app.fragmentmanagerimpl.restoreallstate(fragmentmanager.java:1767) @ android.support.v4.app.fragmentactivity.oncreate(fragmentactivity.java:208) @ de.nathan.android.droidschool.mainactivity.oncreate(mainactivity.java:43) @ android.app.activity.performcreate(activity.java:5133) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) ... 12 more caused by: java.lang.instantiationexception: can't instantiate class de.nathan.android.droidschool.mainactivity$fragmenttab1; no empty constructor @ java.lang.class.newinstanceimpl(native method) @ java.lang.class.newinstance(class.java:1130) @ android.support.v4.app.fragment.instantiate(fragment.java:394) ... 19 more
place
mainactivity.context = getapplicationcontext();
before
msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager());
if thats not working show logcat
Comments
Post a Comment