-1

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)
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
Luke Swain
  • 25
  • 7

2 Answers2

0

Your problem is because you are declaring the TextView too early according to the fragment lifycycle, so, instead of declaring it at onCreateView, declare it at onViewCreated, so it will be:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View loginFragmentView = inflater.inflate(R.layout.login_layout, container, false);
        return loginFragmentView;
    }


  @Override
  public void onViewCreated(View loginFragmentView, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);

      TextView imei = (TextView) loginFragmentView.findViewById(R.id.txt_device_id);
      imei.setText("IMEI: ");
  }
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
0

Looking at your stacktrace I guess your loginFragmentView doesn't have child of type textview with id txt_device_id so check if in inflate method you are passing correct layout name which contains textview with id you are trying to find

Chirag Jain
  • 133
  • 9
  • It is definitely in there. see layout markup in question. – Luke Swain May 22 '16 at 12:08
  • you can check whether your loginViewFragment contains that view or not by putting break point at TextView imei = (TextView) loginFragmentView.findViewById(R.id.txt_device_id); and evaluate expression. check the childs of loginViewFragment – Chirag Jain May 22 '16 at 12:13