QuartZ QuartZ - 4 months ago 127
Android Question

android.view.InflateException Error inflating class fragment, Binary XML file line #1?

I have a working parcelable project and have compared the two. And I seem can't to find the error. The Android Studio doesn't show any syntax error. And after trying to fix it, it seems that the error happen in the activity_main.xml. But i just cant figure it out. The error is InflateException.

And this is the error from the Android Studio Android Monitor:


--------- beginning of crash
07-14 00:03:23.801 13726-13726/com.example.romi.parcelablecreatortest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.romi.parcelablecreatortest, PID: 13726
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.romi.parcelablecreatortest/com.example.romi.parcelablecreatortest.MainActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.romi.parcelablecreatortest.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.romi.parcelablecreatortest.MainActivity.onCreate(MainActivity.java:11) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at com.example.romi.parcelablecreatortest.MainActivityFragment.onCreate(MainActivityFragment.java:74)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2062)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1264)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1366)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2409)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.romi.parcelablecreatortest.MainActivity.onCreate(MainActivity.java:11) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 


Here is the code:

activity_main.xml

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.example.romi.parcelablecreatortest.MainActivityFragment"
android:layout="@layout/activity_main_activity_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


MainActivity.java

package com.example.romi.parcelablecreatortest;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

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


acitivty_main_activity_fragment.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">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>


MainActivityFragment.java

package com.example.romi.parcelablecreatortest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivityFragment extends Fragment {

private TopRatedMovieJsonAdapter topRatedMovieAdapter;

private ArrayList<TopRatedMovieJson> topRatedMovie;

TopRatedMovieJson[] topRatedMovieJson;

// MovieDB API links
String imageBaseURL = "http://image.tmdb.org/t/p/w500"; // Use this to get the image. Add the poster path to this.
String apiKey = "*API KEY*";
String myUrl = "https://api.themoviedb.org/3/movie/550?api_key=";

// json file
String json = " *This is filled with the json file* ";


public void jsonParser(String json) {

final String LOG_TAG = MainActivityFragment.class.getSimpleName();

// The variables that we want from the json file
final String title = "original_title"; // JSONObject
final String posterPath = "poster_path"; // JSONObject
final String synopsis = "overview"; // JSONObject
final String userRating = "popularity"; // JSONObject
final String releaseDate = "release_date"; // JSONObject

// Parsing the json file
try {

JSONArray jsonArray = new JSONArray(json);
// for is used to cycle through the json array.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i); // getJSONObject(i) will take onw of the json array with i=0 as its first index
topRatedMovieJson[i] = new TopRatedMovieJson(
jsonObject.getString(title),
jsonObject.getString(posterPath),
jsonObject.getString(synopsis),
jsonObject.getString(userRating),
jsonObject.getString(releaseDate)
);
}

} catch (JSONException e) {
e.printStackTrace();
}

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

topRatedMovieJson = new TopRatedMovieJson[1000];

try {
JSONArray jsonArray = new JSONArray(json);
jsonParser(jsonArray);
}catch (JSONException e){
e.printStackTrace();
}

if (savedInstanceState == null) {
topRatedMovie = new ArrayList<TopRatedMovieJson>(Arrays.asList(topRatedMovieJson));
} else {
topRatedMovie = savedInstanceState.getParcelableArrayList("parce");
}
}

public MainActivityFragment() {
}

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("parce", topRatedMovie);
super.onSaveInstanceState(outState);
}

/**
* onCreateView for the topRatedMoviejSon variable.
* Where topRatedMoviejSon is a json from moviedb api.
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootview = inflater.inflate(R.layout.activity_main, container, false);

topRatedMovieAdapter = new TopRatedMovieJsonAdapter(getActivity(), topRatedMovie);

ListView listView = (ListView) rootview.findViewById(R.id.listView);
listView.setAdapter(topRatedMovieAdapter);

return rootview;
}
}

Answer

you are not initialised TopRatedMovieJson[] topRatedMovieJson;that's why you got NPEat

java.lang.NullPointerException: storage == null at java.util.Arrays$ArrayList.(Arrays.java:38) at java.util.Arrays.asList(Arrays.java:155) at com.example.romi.parcelablecreatortest.MainActivityFragment.onCreate(MainActivityFragment.java:74)

update in MainActivityFragment's

 public void jsonParser(String json) {

        final String LOG_TAG = MainActivityFragment.class.getSimpleName();

        // The variables that we want from the json file
        final String title = "original_title"; // JSONObject
        final String posterPath = "poster_path"; // JSONObject
        final String synopsis = "overview"; // JSONObject
        final String userRating = "popularity"; // JSONObject
        final String releaseDate = "release_date"; // JSONObject

        // Parsing the json file
        try {
            JSONArray jsonArray = new JSONArray(json);
            // for is used to cycle through the json array.
            topRatedMovieJson= new TopRatedMovieJson[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i); // getJSONObject(i) will take onw of the json array with i=0 as its first index
                topRatedMovieJson[i] = new TopRatedMovieJson(
                        jsonObject.getString(title),
                        jsonObject.getString(posterPath),
                        jsonObject.getString(synopsis),
                        jsonObject.getString(userRating),
                        jsonObject.getString(releaseDate)
                );
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
Comments