T. Rex T. Rex - 6 months ago 43
Android Question

nullpointer exception in fragment code when accessing linear layout

I have an Android application that uses navigation drawer. I want to respond to an onClick event on a linear layout in one of the fragments, accessed by clicking on an item in the navigation drawer.

Here's the fragment code - HomeFragment.java

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

try {
take_photo = (LinearLayout) getView().findViewById(R.id.btn_take_photo);
open_gallery = (LinearLayout) getView().findViewById(R.id.btn_open_gallery);
} catch (NullPointerException npe) {
Log.e("samako", npe.toString(), npe);
}
return rootView;
}


Here's the linear layout

<LinearLayout
android:id="@+id/btn_take_photo"
android:layout_width="match_parent"
android:layout_height="55dp"
android:padding="5dp"
android:orientation="horizontal"
android:weightSum="10"
android:descendantFocusability="blocksDescendants"
android:clickable="true"
android:background="@drawable/presc_bg"
android:layout_margin="10dp">



<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/btn_camera" />

<TextView
android:layout_width="0dp"
android:layout_weight="9"
android:gravity="center"
android:textColor="@color/white"
android:text="Take a picture of your prescription"
android:layout_height="match_parent" />

</LinearLayout>


But as soon as install the app and run, i get an error and the app crashes, at least, until I added a try catch. Here's the error I'm getting:

02-28 15:25:38.598 22386-22386/com.genesis.genapp E/samako﹕ java.lang.NullPointerException
java.lang.NullPointerException
at com.genesis.genapp.fragments.HomeFragment.onCreateView(HomeFragment.java:31)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

Answer

Change to

 take_photo = (LinearLayout) rootView.findViewById(R.id.btn_take_photo);
 open_gallery = (LinearLayout) rootView.findViewById(R.id.btn_open_gallery);

or in onActivityCreated

 take_photo = (LinearLayout) getView().findViewById(R.id.btn_take_photo);
 open_gallery = (LinearLayout) getView().findViewById(R.id.btn_open_gallery);

getView returns null. Calling it too early. Use it in onActivityCreated.