Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

android - Working With Fragments: `Error inflating class fragment`

I'm practicing with Fragments, and created a tester to get familiar with the process.

My goal is to create a simple app that has a ListView made up of 10 strings. The ListView is contained by a fragment within the MainActivity.

However, I have run into an error...

   FATAL EXCEPTION: main
   Process: com.example.frank.listview_test, PID: 5819
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.frank.listview_test/com.example.frank.listview_test.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class fragment
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
       at android.app.ActivityThread.access$1100(ActivityThread.java:221)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:158)
       at android.app.ActivityThread.main(ActivityThread.java:7224)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class fragment
       at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
       at com.example.frank.listview_test.MainActivity.onCreate(MainActivity.java:17)
       at android.app.Activity.performCreate(Activity.java:6876)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
       at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
       at android.os.Handler.dispatchMessage(Handler.java:102)?
       at android.os.Looper.loop(Looper.java:158)?
       at android.app.ActivityThread.main(ActivityThread.java:7224)?
       at java.lang.reflect.Method.invoke(Native Method)?
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
    Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
       at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)?
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)?
       at com.example.frank.listview_test.MainActivity.onCreate(MainActivity.java:17)?
       at android.app.Activity.performCreate(Activity.java:6876)?
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
       at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
       at android.os.Handler.dispatchMessage(Handler.java:102)?
       at android.os.Looper.loop(Looper.java:158)?
       at android.app.ActivityThread.main(ActivityThread.java:7224)?
       at java.lang.reflect.Method.invoke(Native Method)?
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
       at com.example.frank.listview_test.MainActivity$TestListFragment.onCreateView(MainActivity.java:37)
       at android.app.Fragment.performCreateView(Fragment.java:2281)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1141)
       at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1245)
       at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2218)
       at android.app.FragmentController.onCreateView(FragmentController.java:98)
       at android.app.Activity.onCreateView(Activity.java:6070)
       at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
       at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)?
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)?
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)?
       at android.view.LayoutInflater.inflate(LayoutInflater.java:527)?
       at android.view.LayoutInflater.inflate(LayoutInflater.java:429)?
       at android.view.LayoutInflater.inflate(LayoutInflater.java:380)?
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)?
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)?
       at com.example.frank.listview_test.MainActivity.onCreate(MainActivity.java:17)?
       at android.app.Activity.performCreate(Activity.java:6876)?
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)?
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)?
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)?
       at android.app.ActivityThread.access$1100(ActivityThread.java:221)?
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)?
       at android.os.Handler.dispatchMessage(Handler.java:102)?
       at android.os.Looper.loop(Looper.java:158)?
       at android.app.ActivityThread.main(ActivityThread.java:7224)?
       at java.lang.reflect.Method.invoke(Native Method)?
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)?
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)?

Here is my relevant code...

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:background="#FFFFFF"
    android:gravity="center"
    tools:context="com.example.frank.listview_test.MainActivity">

    <fragment android:name="com.example.frank.listview_test.MainActivity$TestListFragment"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

list_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listFragment"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ListView
        android:id="@+id/testListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />

</FrameLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:textSize="50sp"
    android:textColor="#000000"
    android:layout_gravity="center"/>

MainActivity.java:

package com.example.frank.listview_test;

import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

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

    public static class TestListFragment extends Fragment {

        private String[] myStringArray = new String[20];
        private ListView listView;

        private void setArray() {
            for (int i = 0; i < 10; i++) {
                this.myStringArray[i] = "hello";
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            setArray();

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item , myStringArray);
            listView = (ListView) getActivity().findViewById(R.id.testListView);
            listView.setAdapter(adapter);

            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.list_fragment, container, false);
        }
    }
}

Is there any bit of this code that stands out as wrong that could be causing this error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your fragment code onCreateView() should first inflate the list_fragment layout that contains the ListView. Then you should be calling findViewById() on the inflated layout and not on the activity since the fragment layout is not yet a part of the activity view hierarchy.

For example:

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_fragment, container, false);
listView = (ListView) view.findViewById(R.id.testListView);
listView.setAdapter(adapter);

return view;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...