当前位置: 首页 > 知识库问答 >
问题:

Android.View.InflateExceptioninflating类片段时出错,二进制XML文件行#1?

孙帅
2023-03-14

Android项目给出InflateException错误。

-------崩溃开始07-14 00:03:23.801 13726-13726/com.example.romi.ParcelableCreatorTest E/AndroidRuntime:致命异常:主进程:com.example.romi.ParcelableCreatorTest,pid:13726 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.romi.ParcelableCreatorTest/com.example.romi.ParcelableCreatorTest.MainActivity}:Android.View.InflateException:二进制XML文件行#1:在com.android.internal.os.zygoteinit.main(zygoteinit.java:616)处init$MethodAndargscaller.run(zygoteinit.java:726)由:android.view.inflater.java:616)引起的异常:二进制XML文件行#1:二进制XML文件行#1:在android.view.layoutinflater.inflate.java:539)在android.view.layoutinflater.inflate(layoutinflater.java:423)在

代码如下:

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个答案

姜羽
2023-03-14

您没有初始化topratedmoviejson[]topratedmoviejson;这就是您在以下位置获得npe的原因

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)

MainActivityFragment中更新

 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();
        }
    }
 类似资料: