android - ListView.addHeaderView(View) Crash on Api 11 and API12 -


i have list view need add header view , footer view,

it runs on api else 11 & 12.

this log cat

java.lang.illegalstateexception: cannot add header view list -- setadapter has been called. 11-13 11:21:13.717: e/androidruntime(583): @     android.widget.listview.addheaderview(listview.java:255) 11-13 11:21:13.717: e/androidruntime(583): @ mediafragment.setadapter_(mediafragment.java:1041) 11-13 11:21:13.717: e/androidruntime(583): @    mediafragment.jsonobjectonsuccess(mediafragment.java:991) 11-13 11:21:13.717: e/androidruntime(583): @ mediafragment.getcache_db(mediafragment.java:2074) 11-13 11:21:13.717: e/androidruntime(583): @ databaseadapter$getasynctask.onpostexecute(databaseadapter.java:253) 11-13 11:21:13.717: e/androidruntime(583): @ databaseadapter$getasynctask.onpostexecute(databaseadapter.java:1) 13 11:21:13.717: e/androidruntime(583): @ android.os.asynctask.finish(asynctask.java:590)  11-13 11:21:13.717: e/androidruntime(583): @ android.os.asynctask.access$600(asynctask.java:149)   11-13 11:21:13.717: e/androidruntime(583): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:603)  11-13 11:21:13.717: e/androidruntime(583): @ android.os.handler.dispatchmessage(handler.java:99)  11-13 11:21:13.717: e/androidruntime(583): @ android.os.looper.loop(looper.java:132)  11-13 11:21:13.717: e/androidruntime(583): @ android.app.activitythread.main(activitythread.java:4025)        11-13 11:21:13.717: e/androidruntime(583): @ java.lang.reflect.method.invokenative(native method)    11-13 11:21:13.717: e/androidruntime(583): @ java.lang.reflect.method.invoke(method.java:491)  11-13 11:21:13.717: e/androidruntime(583): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841)  11-13 11:21:13.717: e/androidruntime(583): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:599)   11-13 11:21:13.717: e/androidruntime(583): @ dalvik.system.nativestart.main(native method) 

and code :

    listview = (listview) view.findviewbyid(r.id.fragmentlistview);      headlistview = (view) mactivity.getlayoutinflater().inflate(             r.layout.headlistview,listview,false);     footerlistview = (view) mactivity.getlayoutinflater().inflate(             r.layout.media_footer,listview,false);       listview.addheaderview(headlistview, null, false);     listview.addfooterview(footerlistview, null, false);      listview.setadapter(mediaadapter);     mediaadapter.notifydatasetchanged();     listview.onrestoreinstancestate(state); 

the error says: "cannot add header view list -- setadapter has been called.". means need add header before setting adapter.

also try overriding factory method following. solved problem.

public void addheaderview(@nonnull view view){     if (build.version.sdk_int >= build.version_codes.lollipop) {         super.addheaderview(view);     } else {         try {             if (!(view.getlayoutparams() instanceof abslistview.layoutparams)) {                 view.setlayoutparams(new layoutparams(viewgroup.layoutparams.match_parent                         , viewgroup.layoutparams.wrap_content));             }             super.addheaderview(view);         } catch (exception e) {             e.printstacktrace();         }     } } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -