2

Android project gives InflateException error.

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)

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);
    }
}

1 Answer 1

1

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();
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I initialize it in onCreate. topRatedMovieJson = new TopRatedMovieJson[1000];
But it still doesn't solve the InflateException error. Any more solution to try?
@QuartZ I don't have time for solve your full application's errors . first read full logcat and try to error by yourself . if you got error in other line then edit your question.
Please try cleaning your project and rebuild it!
Thanks. I fix it. The problem is in the xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.