Below is my code for a fragment I have, I am trying to set the text of a textView in the view, I was under the impression that as long as you inflate the view first, it would not return null as it has been created. That according to the fragment life cycle.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View loginFragmentView = inflater.inflate(R.layout.login_layout, container, false);
TextView imei = (TextView) loginFragmentView.findViewById(R.id.txt_device_id);
imei.setText("IMEI: ");
return loginFragmentView;
}
This does cause the application to crash with the nullPointerException, does anyone know how to get around this. Is there a method I can override that will only be called one the fragment lifecycle has finished.
I also tried to put it in onResume() as that is right at the end of the lifecycle buthad same result. Any Idea?
EDIT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Your device ID:"
android:id="@+id/txt_id_title"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Fetching ID..."
android:id="@+id/txt_device_id"
android:layout_below="@+id/txt_id_title"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ERROR LOG
05-22 19:40:02.230 2799-2799/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: alertssystem.rrr.lsa13.rrr_alerts, PID: 2799
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.TextView.toString()' on a null object reference
at alertssystem.rrr.lsa13.rrr_alerts.login_layout_fragment.onCreateView(login_layout_fragment.java:24)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)