android - Views in Fragment are null -
im still working on android app. im facing problem:
code:
package smoca.ch.kreagen.fragments; import android.content.context; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import io.realm.realm; import io.realm.realmquery; import smoca.ch.kreagen.r; import smoca.ch.kreagen.models.idea; public class singleideafragment extends fragment{ private textview title; private textview owner; private textview description; private realm realm; private idea idea; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view layout = inflater.inflate(r.layout.single_idea_fragment_layout, container, false); // inflate layout fragment idea = getsingleidea(getactivity().getbasecontext()); title = (textview) container.findviewbyid(r.id.singleideatitle); owner = (textview) container.findviewbyid(r.id.singleideaowner); description = (textview) container.findviewbyid(r.id.singleideadescription); title.settext(idea.gettitle()); owner.settext(idea.getownerid()); description.settext(idea.gettext()); return layout; } public idea getsingleidea(context ctx) { realm = realm.getinstance(ctx); realmquery<idea> ideaquery = realm.where(idea.class); idea = ideaquery.findfirst(); return idea; } }
error:
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference @ smoca.ch.kreagen.fragments.singleideafragment.oncreateview(singleideafragment.java:35)
i know, problem textviews (title, owner, description) null.. dont know why. assigned them textview's layout, referencing id's.
what doing wrong?
thanks & greetings john
when called inflater.inflate
, specified third parameter, attachtoroot, false
. means gets returned inflater view r.layout.single_idea_fragment_layout
. thus, findviewbyid
should called on layout
rather container
title = (textview) layout.findviewbyid(r.id.singleideatitle); owner = (textview) layout.findviewbyid(r.id.singleideaowner); description = (textview) layout.findviewbyid(r.id.singleideadescription);
Comments
Post a Comment