android - Accessing a view from a fragment -
if take access textview out of java code works. i'm assuming made mistake somewhere it. can see problem?
package com.projectcaruso.naturalfamilyplaning; import com.projectcaruso.naturalfamilyplanning.r; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class historyfragment extends fragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sqlhelper entry = new sqlhelper(getactivity()); textview tv = (textview) getview().findviewbyid(r.id.tvsqlinfo); entry.open(); string results = entry.getdata(); entry.close(); tv.settext(results); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_history, container, false); } }
xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation ="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <tablelayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <tablerow> <textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="date" /> <textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="temp" /> </tablerow> </tablelayout> <textview android:id="@+id/tvsqlinfo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="get info db" /> </linearlayout>
logcat:
05-12 22:55:01.799: e/androidruntime(1983): fatal exception: main 05-12 22:55:01.799: e/androidruntime(1983): java.lang.nullpointerexception 05-12 22:55:01.799: e/androidruntime(1983): @ com.projectcaruso.naturalfamilyplaning.historyfragment.oncreate(historyfragment.java:20) 05-12 22:55:01.799: e/androidruntime(1983): @ android.support.v4.app.fragment.performcreate(fragment.java:1437) 05-12 22:55:01.799: e/androidruntime(1983): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:877) 05-12 22:55:01.799: e/androidruntime(1983): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1088) 05-12 22:55:01.799: e/androidruntime(1983): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) 05-12 22:55:01.799: e/androidruntime(1983): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1444) 05-12 22:55:01.799: e/androidruntime(1983): @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:429) 05-12 22:55:01.799: e/androidruntime(1983): @ android.os.handler.handlecallback(handler.java:725) 05-12 22:55:01.799: e/androidruntime(1983): @ android.os.handler.dispatchmessage(handler.java:92) 05-12 22:55:01.799: e/androidruntime(1983): @ android.os.looper.loop(looper.java:137) 05-12 22:55:01.799: e/androidruntime(1983): @ android.app.activitythread.main(activitythread.java:5041) 05-12 22:55:01.799: e/androidruntime(1983): @ java.lang.reflect.method.invokenative(native method) 05-12 22:55:01.799: e/androidruntime(1983): @ java.lang.reflect.method.invoke(method.java:511) 05-12 22:55:01.799: e/androidruntime(1983): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-12 22:55:01.799: e/androidruntime(1983): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-12 22:55:01.799: e/androidruntime(1983): @ dalvik.system.nativestart.main(native method)
the problem call getview()
returns null
.
why that?
because oncreate()
called before oncreateview()
.
have @ fragment lifecycle more information: http://developer.android.com/guide/components/fragments.html#creating
solution:
move logic oncreateview()
method , keep reference inflated view instead of calling via getview()
. this:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_history, container, false); sqlhelper entry = new sqlhelper(getactivity()); entry.open(); string results = entry.getdata(); entry.close(); textview tv = (textview) view.findviewbyid(r.id.tvsqlinfo); tv.settext(results); return view; }
Comments
Post a Comment