android - Null Object Reference on TextView in getView() / fragment -


hi there try create fragment class:

import android.support.v4.app.fragment; public class updatesfragment extends fragment { textview output ;  @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     super.oncreate(savedinstancestate);     view view = inflater.inflate(r.layout.updates_layout, null, false);     output = (textview) getview().findviewbyid(r.id.jsondata);     return view;     } } 

but when run app on device, app break.

on android studio editor:

method invocation 'getview().findviewbyid(r.id.jsondata)' may produce 'java.lang.nullpointerexception'

on device debuger:

java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference

and here xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin"     tools:context=".updatesfragment"      android:background="#24ff93">      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="hackpundit blogs"         android:textstyle="bold"         android:id="@+id/heading"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true"         android:textsize="38dp"         android:textcolor="#230cff" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="large text"         android:textsize="15dp"         android:textstyle="bold"         android:id="@+id/jsondata"         android:layout_margintop="42dp"         android:layout_below="@+id/heading"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:textcolor="#000000" />   </relativelayout> 

right way view this:

view view = inflater.inflate(r.layout.updates_layout, null, false); output = (textview) view.findviewbyid(r.id.jsondata); 

you have use inflated view find views instead of getview()


Comments