简体   繁体   中英

Android debugging error android.view.InflateException: Binary XML file line #9: Error inflating class fragment

I keep getting this weird error when i try to run my app on my phone: android.view.InflateException: Binary XML file line #9: Error inflating class fragment and i don't know why. I think maybe its to do with the way i'm calling fragments? I've alsready searched stack overflow for an answer but all of them seem to relate to maps


XML1

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.adrian.desktop.login"
    tools:ignore="MergeRootFrame">

<fragment
android:id="@+id/headlines_fragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_login" />
 </FrameLayout>

XML 2

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.adrian.desktop.ViewProgress$PlaceholderFragment">


    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <LinearLayout
            android:id="@+id/Graph"
            android:orientation="vertical"
            android:fillViewport="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            </LinearLayout>
            </ScrollView>
</RelativeLayout>

java 1

package com.example.adrian.desktop;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class login extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        TextView loginTXT = (TextView) findViewById(R.id.logintxt);
        Typeface robotoTHIN = Typeface.createFromAsset(getAssets(),"Fonts/Roboto-Thin.tff");
        loginTXT.setTypeface(robotoTHIN);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_login, container, false);
            return rootView;
        }
    }
}

java 2

package com.example.adrian.desktop;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

public class ViewProgress extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_progress);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        // init example series data
        GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
                new GraphViewData(1, 2.0d)
                , new GraphViewData(2, 1.5d)
                , new GraphViewData(3, 2.5d)
                , new GraphViewData(4, 1.0d)
        });

        GraphView graphView = new LineGraphView(
                this // context
                , "GraphViewDemo" // heading
        );
        graphView.addSeries(exampleSeries); // data

        LinearLayout layout = (LinearLayout) findViewById(R.id.Graph);
        layout.addView(graphView);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.view_progress, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_view_progress, container, false);
            return rootView;
        }
    }
}

StackTrace:

09-27 18:52:58.122  10725-10725/com.example.adrian.desktop E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.adrian.desktop, PID: 10725
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adrian.desktop/com.example.adrian.desktop.login}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5867)
            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:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            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:377)
            at android.app.Activity.setContentView(Activity.java:1997)
            at com.example.adrian.desktop.login.onCreate(login.java:19)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5867)
            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:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException: name == null
            at java.lang.VMClassLoader.findLoadedClass(Native Method)
            at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
            at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:438)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:256)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            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:377)
            at android.app.Activity.setContentView(Activity.java:1997)
            at com.example.adrian.desktop.login.onCreate(login.java:19)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5867)
            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:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)

The stack trace identifies the issue pretty clearly:

java.lang.NullPointerException: name == null

You didn't define the Fragment you want to have inflated. Look at examples here , the fragment needs a name. For example

android:name="com.example.news.ArticleListFragment"

If you don't provide the name, the system has no way of determining what to inflate.

You need to address the fragment itself (via the name attibute) as you can see in the example from the documentation :

<fragment android:name="com.example.android.fragments.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

Your soution should be this here:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.adrian.desktop.login"
    tools:ignore="MergeRootFrame">

    <fragment
        android:name="com.example.adrian.desktop.login$PlaceholderFragment"
        android:id="@+id/headlines_fragment"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_login" />

</FrameLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM