Error Inflating Class Fragment Causing Android View InflateException and Activity ComponentInfo Start Failure

Despite trying various answers in this question, none of them seem to resolve the problem. Additionally, obtaining arguments from a fragment results in a null pointer exception. The onclick interface is present in the main fragment, and the details activity and its layout are also available. The details fragment is included, along with its layout. The package in question is com.example.freewaresys.sunshine. The logcat is also present. One solution involves using the fragment’s view to reference items in the class and moving the code from one section to another. Another solution involves adding the fragment name for the particular item. You could also try adding android_value=”@integer/google_play_services_version” />. However, the app crashes when attempting to open the details activity after clicking on a recycler view item.


Question:

The app functions properly, but it crashes when I attempt to open the details activity by clicking on an item in the recycler view.

Despite trying numerous solutions from the answers provided in this question, I have been unable to resolve this issue. Additionally, I encountered a null pointer exception while retrieving arguments from the fragment.

The interface attached to the click event in the main fragment.

public void onClick(long date) {
    Uri todayUri = WeatherContract.WeatherEntry.buildWeatherUriWithDate(date);
    DetailsFragment detailsFragment = (DetailsFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.details_fragment);
    if (todayUri != null) {
        String myUri = todayUri.toString();
        if (detailsFragment != null) {
            DetailsFragment detailsFragment1 = new DetailsFragment();
            Bundle bundle = new Bundle();
            bundle.putString(DetailsFragment.FRAGMENT_KEY, myUri);
            detailsFragment1.setArguments(bundle);
            detailsFragment = detailsFragment1;
            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.details_fragment, detailsFragment).commit();
        } else {
            Intent detailsActivity = new Intent();
            detailsActivity.setClass(getActivity(), DetailsActivity.class);
            detailsActivity.setData(todayUri);
            startActivity(detailsActivity);
        }
    }
}

my details activity

package com.example.freewaresys.sunshine;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class DetailsActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Uri uri = getIntent().getData();
    String myUri = uri.toString();
    Bundle bundle = new Bundle();
    bundle.putString(DetailsFragment.FRAGMENT_KEY, myUri);
    DetailsFragment detailsFragment = new DetailsFragment();
    detailsFragment.setArguments(bundle);
    getSupportFragmentManager().beginTransaction()
            .add(R.id.activity_details_fragment, detailsFragment).commit();
}

}

my detail activity layout



my details fragment

The code belongs to the “com.example.freewaresys.sunshine” package.

    import android.content.Context;
    import android.database.Cursor;
    import android.databinding.DataBindingUtil;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.CursorLoader;
    import android.support.v4.content.Loader;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.support.v4.app.Fragment;
    import android.widget.ImageView;
    import android.widget.TextView;
    import com.example.freewaresys.sunshine.data.WeatherContract;
    import com.example.freewaresys.sunshine.databinding.FragmentDetailsBinding;
    import com.example.freewaresys.sunshine.sunshineUtils.SunshineDateUtils;
    import com.example.freewaresys.sunshine.sunshineUtils.SunshineWeatherUtils;
    public class DetailsFragment extends Fragment implements
    LoaderManager.LoaderCallbacks {
    public static final String FRAGMENT_KEY = "URI";
    public static final String[] WeatherDetailsProjection = {
            WeatherContract.WeatherEntry.COLUMN_DATE,
            WeatherContract.WeatherEntry.COLUMN_TEMP_MAX,
            WeatherContract.WeatherEntry.COLUMN_TEMP_MIN,
            WeatherContract.WeatherEntry.COLUMN_WEATHER_CONDITION,
            WeatherContract.WeatherEntry.COLUMN_HUMIDITY,
            WeatherContract.WeatherEntry.COLUMN_PRESSURE,
            WeatherContract.WeatherEntry.COLUMN_WIND_SPEED
    };
    public static final int INDEX_WEATHER_DATE = 0;
    public static final int INDEX_TEMP_MAX = 1;
    public static final int INDEX_TEMP_MIN = 2;
    public static final int INDEX_WEATHER_CONDITION = 3;
    public static final int INDEX_WEATHER_HUMIDITY = 4;
    public static final int INDEX_WEATHER_PRESSURE = 5;
    public static final int INDEX_WEATHER_WIND_SPEED = 6;
    public static final int LOADER_ID = 23;
    private Uri mUri;
    TextView date;
    TextView weatherDescrirption;
    ImageView weatherIcon;
    TextView highTemperature;
    TextView lowTemperature;
    TextView humidity;
    TextView pressure;
    TextView windMeasurement;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        String uriString = bundle.getString(FRAGMENT_KEY);
        mUri = Uri.parse(uriString);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_details, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        date = (TextView) getActivity().findViewById(R.id.date);
        weatherDescrirption = (TextView) getActivity().findViewById(R.id.weather_description);
        weatherIcon = (ImageView) getActivity().findViewById(R.id.weather_icon);
        lowTemperature = (TextView) getActivity().findViewById(R.id.low_temperature);
        highTemperature = (TextView) getActivity().findViewById(R.id.high_temperature);
        humidity = (TextView) getActivity().findViewById(R.id.humidity);
        pressure = (TextView) getActivity().findViewById(R.id.pressure);
        windMeasurement = (TextView) getActivity().findViewById(R.id.wind_measurement);
        getActivity().getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    }
    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(getActivity(),
                mUri,
                WeatherDetailsProjection,
                null,
                null,
                null
        );
    }
    @Override
    public void onLoadFinished(Loader loader, Cursor mCursor) {
        boolean cursorHasValidData = false;
        if (mCursor != null && mCursor.moveToFirst()) {
        /* We have valid data, continue on to bind the data to the UI */
            cursorHasValidData = true;
        }
        if (!cursorHasValidData) {
        /* No data to display, simply return and do nothing */
            return;
        }
        long datee = mCursor.getLong(INDEX_WEATHER_DATE);
        String friendlyDate = SunshineDateUtils.getFriendlyDateString(getActivity(), datee);
        date.setText(friendlyDate);
        String condition = mCursor.getString(INDEX_WEATHER_CONDITION);
        weatherDescrirption.setText(condition);
        int weatherImageResource = SunshineWeatherUtils.getLargeArtResourceIdForWeatherCondition(condition);
        weatherIcon.setImageResource(weatherImageResource);
        double maxTemp = mCursor.getDouble(INDEX_TEMP_MAX);
        String maxTemprature = SunshineWeatherUtils.formateTemperature(getActivity(), maxTemp);
        highTemperature.setText(maxTemprature);
        double minTemp = mCursor.getDouble(INDEX_TEMP_MIN);
        String minTemprature = SunshineWeatherUtils.formateTemperature(getActivity(), minTemp);
        lowTemperature.setText(minTemprature);
        float humidityy = mCursor.getFloat(INDEX_WEATHER_HUMIDITY);
        String weathereHumidity = String.format(this.getString(R.string.format_humidity), humidityy);
        humidity.setText(weathereHumidity);
        float pressuree = mCursor.getFloat(INDEX_WEATHER_PRESSURE);
        String weatherePressure = String.format(this.getString(R.string.format_pressure), pressuree);
        pressure.setText(weatherePressure);
        float wind = mCursor.getFloat(INDEX_WEATHER_WIND_SPEED);
        String weathereWind = String.format(this.getString(R.string.format_wind_kmh), wind);
        windMeasurement.setText(weathereWind);
    }
    @Override
    public void onLoaderReset(Loader loader) {
    }
    }

my details fragment layout



    
    

my logcat

.freewaresys.sunshine E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.freewaresys.sunshine, PID: 23552
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.freewaresys.sunshine/com.example.freewaresys.sunshine.DetailsActivity}: android.view.InflateException: Binary XML file line #4: Error inflating class fragment
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                  at android.os.Looper.loop(Looper.java:193)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                  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:829)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                               Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class fragment
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341)
                                                                                  at android.app.Activity.setContentView(Activity.java:1948)
                                                                                  at com.example.freewaresys.sunshine.DetailsActivity.onCreate(DetailsActivity.java:12)
                                                                                  at android.app.Activity.performCreate(Activity.java:5343)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                  at android.os.Looper.loop(Looper.java:193) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                  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:829) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 
                                                                               Caused by: java.lang.NullPointerException
                                                                                  at com.example.freewaresys.sunshine.DetailsFragment.onCreateView(DetailsFragment.java:66)
                                                                                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1255)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1472)
                                                                                  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1691)
                                                                                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3440)
                                                                                  at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
                                                                                  at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                                                                                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                                  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341) 
                                                                                  at android.app.Activity.setContentView(Activity.java:1948) 
                                                                                  at com.example.freewaresys.sunshine.DetailsActivity.onCreate(DetailsActivity.java:12) 
                                                                                  at android.app.Activity.performCreate(Activity.java:5343) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                  at android.os.Looper.loop(Looper.java:193) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                  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:829) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 



Solution 1:

When working with the

DetailsFragment

class, make use of the fragment’s view to access the necessary items. Additionally, relocate the code from its current position in

onActivityCreated

to

onCreateView()

.

Code snippet:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_details, container, false);
    Bundle bundle = this.getArguments();
    String uriString = bundle.getString(FRAGMENT_KEY);
    mUri = Uri.parse(uriString);
    // Inflate the layout for this fragment
    date = (TextView) v.findViewById(R.id.date);
    weatherDescrirption = (TextView) v.findViewById(R.id.weather_description);
    weatherIcon = (ImageView) v.findViewById(R.id.weather_icon);
    lowTemperature = (TextView) v.findViewById(R.id.low_temperature);
    highTemperature = (TextView) v.findViewById(R.id.high_temperature);
    humidity = (TextView) v.findViewById(R.id.humidity);
    pressure = (TextView) v.findViewById(R.id.pressure);
    windMeasurement = (TextView) v.findViewById(R.id.wind_measurement);
    getActivity().getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}


Solution 2:


In order to include a fragment, you need to assign a name to it using

<fragment>

, such as

class

.




Solution 3:

Could you please try by adding

The value for the Android attribute is set to the integer named “google_play_services_version“.

Frequently Asked Questions