NullPointerException when accessing view from onCreate()

Asked 2 years ago, Updated 2 years ago, 79 views

This is one of the typical questions that is frequently asked in hashcode

You have created a new activity using the wizard according to the tutorial. NullPointerException occurred when a method call was attempted through View (acquired with findViewById() method) within onCreate() of Activity.

Activity's onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View something = findViewById(R.id.something);
    something.setOnClickListener(new View.OnClickListener() { ... }); // NPE 

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

Layout XML (fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="packagename.MainActivity$PlaceholderFragment" >

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/something" />

</RelativeLayout>

android nullpointerexception java android-fragments

2022-09-22 10:44

1 Answers

The tutorial looks old. Create activity-based UI instead of fragment-based UI that prefers the code generated by Wizard.

The current view is in fragment layout (fragment_main.xml), not activity layout ((activity_main.xml)). OnCreate() is too early in the app's lifecycle to find that view in the Activity view hierarchy. So the null value would have been returned. I think the NPE occurred because I tried to call the method while it was null.

The most recommended method is to call the findViewById() method in the activated fragment layout rootView after moving the code to fragment onCreateView().

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_main, container,
      false);

  View something = rootView.findViewById(R.id.something); // not activity findViewById()
  something.setOnClickListener(new View.OnClickListener() { ... });

  return rootView;
}

In addition, the fragment layout will eventually be part of the activity view hierarchy and can also be found by the findViewById() method within the activity. Only after the fragment transaction is carried out. The remaining fragment transactions are performed in super.onStart() after the onCreate() method is performed.


2022-09-22 10:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.