android - Returning a ListView from OnCreateView in a Fragment -
the following code makes app crash on start:
public class listfragment extends fragment { listview list; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { listview view = (listview) inflater.inflate(r.layout.list_layout, container, false); view.setadapter(new myadapter(getactivity())); return view; } }
however, code:
public class listfragment extends fragment { listview list; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.list_layout, container, false); return view; } }
runs fine, doesn't want, because doesn't show listview. can see reason why might happen?
this log when run first code:
11-19 12:28:58.453 3244-3244/com.example.dan.myapplication e/androidruntime﹕ fatal exception: main java.lang.classcastexception: android.widget.linearlayout cannot cast android.widget.listview @ com.example.dan.myapplication.listfragment.oncreateview(listfragment.java:31) @ android.support.v4.app.fragment.performcreateview(fragment.java:1478) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:927) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:472) @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) @ android.support.v4.view.viewpager.populate(viewpager.java:1068) @ android.support.v4.view.viewpager.populate(viewpager.java:914) @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1436) @ android.view.view.measure(view.java:15518) @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:681) @ android.widget.relativelayout.onmeasure(relativelayout.java:461) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ android.view.view.measure(view.java:15518) @ android.widget.linearlayout.measurevertical(linearlayout.java:847) @ android.widget.linearlayout.onmeasure(linearlayout.java:588) @ android.view.view.measure(view.java:15518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2176) @ android.view.view.measure(view.java:15518) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1874) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1089) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1265) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:989) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4351) @ 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:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method)
and xml of list fragment:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"> </listview> </linearlayout>
i have xml of row in list 2 textviews , imageview listadapter inflates in getview().
use view
findviewbyid
listview
:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.list_layout, container, false); list = (listview) view.findviewbyid(r.id.list); //set adapter, etc return view; }
Comments
Post a Comment