Java android.view.InflateException:二进制 XML 文件第 10 行:错误膨胀类片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23424040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:56:19  来源:igfitidea点击:

android.view.InflateException: Binary XML file line #10: Error inflating class fragment

javaandroidxmlandroid-fragments

提问by Michal

I created an app by following the tutorial at http://www.techotopia.com/index.php/Using_Fragments_in_Android_-_A_Worked_Example, but I have a error.

我按照http://www.techotopia.com/index.php/Using_Fragments_in_Android_-_A_Worked_Example 上的教程创建了一个应用程序,但出现错误。

LogCat:

日志猫:

05-02 08:16:22.044: D/dalvikvm(1846): Late-enabling CheckJNI
05-02 08:16:22.080: D/AndroidRuntime(1846): Shutting down VM
05-02 08:16:22.080: W/dalvikvm(1846): threadid=1: thread exiting with uncaught exception (group=0xa4d81b20)
05-02 08:16:22.096: E/AndroidRuntime(1846): FATAL EXCEPTION: main
05-02 08:16:22.096: E/AndroidRuntime(1846): Process: com.example.myfragmentexample, PID: 1846
05-02 08:16:22.096: E/AndroidRuntime(1846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfragmentexample/com.example.myfragmentexample.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.ActivityThread.access0(ActivityThread.java:135)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.os.Looper.loop(Looper.java:136)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at java.lang.reflect.Method.invoke(Method.java:515)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at dalvik.system.NativeStart.main(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Activity.setContentView(Activity.java:1929)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at com.example.myfragmentexample.MainActivity.onCreate(MainActivity.java:12)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Activity.performCreate(Activity.java:5231)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 08:16:22.096: E/AndroidRuntime(1846):     ... 11 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragmentexample.ToolbarFragment: make sure class name exists, is public, and has an empty constructor that is public
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Fragment.instantiate(Fragment.java:597)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Fragment.instantiate(Fragment.java:561)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Activity.onCreateView(Activity.java:4778)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-02 08:16:22.096: E/AndroidRuntime(1846):     ... 21 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.fragmentexample.ToolbarFragment" on path: DexPathList[[zip file "/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1, /system/lib]]
05-02 08:16:22.096: E/AndroidRuntime(1846):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-02 08:16:22.096: E/AndroidRuntime(1846):     at android.app.Fragment.instantiate(Fragment.java:583)
05-02 08:16:22.096: E/AndroidRuntime(1846):     ... 24 more

MainActivity.java:

主活动.java:

package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity implements
        ToolbarFragment.ToolbarListener {

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


    public void onButtononClick(int fontsize, String text) {
        TextFragment textfragment = (TextFragment) getSupportFragmentManager()
                .findFragmentById(R.id.text_fragment);
        textfragment.zmienWlasciwosci(fontsize, text);

    }

}

activity_main.xml:

活动_main.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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/toolbar_fragment"
        android:name="com.example.fragmentexample.ToolbarFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        tools:layout="@layout/toolbar_fragment" />

    <fragment
        android:id="@+id/text_fragment"
        android:name="com.example.fragmentexample.TextFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        tools:layout="@layout/text_fragment" />

</RelativeLayout>

ToolbarFragment.java:

工具栏片段.java:

package com.example.myfragmentexample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ToolbarFragment extends Fragment implements
        OnSeekBarChangeListener {

    private static int seekvalue = 10;
    private static EditText edittext;

    ToolbarListener activityCallback;

    public interface ToolbarListener {
        public void onButtononClick(int position, String text);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            activityCallback = (ToolbarListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ToolbarListener");
        }
    }

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

        View view = inflater.inflate(R.layout.toolbar_fragment, container,
                false);

        edittext = (EditText) view.findViewById(R.id.editText);
        SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar);
        seekbar.setOnSeekBarChangeListener(this);
        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                buttonClicked(v);

            }
        });

        return view;
    }

    public void buttonClicked(View view) {
        activityCallback.onButtononClick(seekvalue, edittext.getText()
                .toString());

    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        seekvalue = arg1;

    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

}

toolbar_fragment.xml:

toolbar_fragment.xml:

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

    <EditText 
        android:id="@+id/editText" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="text">
        <requestFocus />
    </EditText>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText"
        android:layout_marginTop="14dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:layout_below="@+id/seekBar"
        android:text="@string/button_text" />

</RelativeLayout>

TextFragment.java:

文本片段.java:

package com.example.myfragmentexample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TextFragment extends Fragment {
    private static TextView textview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.text_fragment, container, false);

        textview = (TextView) view.findViewById(R.id.textView1);

        return view;
    }

    public void zmienWlasciwosci (int fontsize, String text){
        textview.setTextSize(fontsize);
        textview.setText(text);
    }

}

text_fragment.xml:

text_fragment.xml:

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

    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="@string/text_label"
       android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout>

AndroidManifest:

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfragmentexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myfragmentexample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I read a lot of posts about this error, but I couldn't find anything wrong in my code. I extended android.support.v4.app.FragmentActivity.

我阅读了很多关于此错误的帖子,但在我的代码中找不到任何错误。我扩展了android.support.v4.app.FragmentActivity

采纳答案by Raghunandan

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.fragmentexample.ToolbarFragment" on path: DexPathList[[zip file "/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1, /system/lib]]

引起:java.lang.ClassNotFoundException:在路径上找不到类“com.example.fragmentexample.ToolbarFragment”:DexPathList[[zip file “/data/app/com.example.myfragmentexample-1.apk”],nativeLibraryDirectories =[/data/app-lib/com.example.myfragmentexample-1, /system/lib]]

This

这个

android:name="com.example.fragmentexample.ToolbarFragment"

Must be

必须是

android:name="com.example.myfragmentexample.ToolbarFragment"

cause package name for ToolbarFragment.javais

原因包名ToolbarFragment.java

package com.example.myfragmentexample;

Similarly for TextFragment

同样对于 TextFragment

android:name="com.example.myfragmentexample.TextFragment"

回答by Shikhar

Try adding correct layouts at correct place like ex. in my case I was putting colors.xmlin values-w820pthen it was giving runtime exception of classtype. I changed the folder from this to simple value and... taddda!! it worked! :)

尝试在正确的位置添加正确的布局,例如 ex。在我的情况下,我将colors.xml放在values-w820p 中,然后它给出了类类型的运行时异常。我将文件夹从这个更改为简单值,然后... taddda !有效!:)

回答by pgcan

I am also new to Android and encountered the same error today. I could not find a solution that worked for me but one thing was clear to me that this issue occurs when android is not able to resolve a mandatory attribute of one of the elements in layout xml.

我也是 Android 新手,今天遇到了同样的错误。我找不到适合我的解决方案,但我很清楚,当 android 无法解析布局 xml 中元素之一的强制属性时,会出现此问题。

So I checked my layout thoroughly and found that by mistake I created an ID

所以我彻底检查了我的布局,发现我错误地创建了一个 ID

<item type="id" name="login"/>

<item type="id" name="login"/>

in ids.xml. This Id was conflicting with attribute

在 ids.xml 中。此 ID 与属性冲突

android:imeActionId="@+id/login"

android:imeActionId="@+id/login"

of EditText element.

的 EditText 元素。

Hope this helps someone.

希望这可以帮助某人。

回答by CoolMind

In my case Google Maps were not correctly initialized. A message in log console printed about it. I had to add

在我的情况下,谷歌地图没有正确初始化。日志控制台中打印了一条关于它的消息。我不得不添加

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />

to <application>inside AndroidManifest.

<application>AndroidManifest 里面。