修复 android.view.InflateException

Fixing android.view.InflateException

请检查下面的代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context="com.github.ppartisan.graphish.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Low"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>

            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/low_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/low_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mid"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>

            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/mid_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/mid_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="High"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>

            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/high_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/high_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

MainActivity.java

package com.example.graphs.viewchart;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mLowLabel, mMidLabel, mHighLabel;
    private BarView mLowBar, mMidBar, mHighBar;

    //Some sample percentage values
    private final int low = 17;
    private final int mid = 90;
    private final int high = 34;

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

        mLowBar = (BarView) findViewById(R.id.low_bar);
        mMidBar = (BarView) findViewById(R.id.mid_bar);
        mHighBar = (BarView) findViewById(R.id.high_bar);

        mLowLabel = (TextView) findViewById(R.id.low_text);
        mMidLabel = (TextView) findViewById(R.id.mid_text);
        mHighLabel = (TextView) findViewById(R.id.high_text);

        mLowBar.set(Color.BLUE, low);
        mMidBar.set(Color.RED, mid);
        mHighBar.set(Color.GREEN, high);

        mLowLabel.setText(getPercentage(low));
        mMidLabel.setText(getPercentage(mid));
        mHighLabel.setText(getPercentage(high));

    }

    private String getPercentage(int per) {
        return per + "%";
    }
}

BarView.java

package com.example.graphs.viewchart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Yohan on 1/11/2017.
 */

public final class BarView extends View {

    private final Paint mPaint = new Paint();
    private int percent = 0;

    public BarView(Context context) {
        super(context);
        init();
    }

    public BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    public void set(int color, int percent) {

        if(percent < 0 || percent > 100) {
            throw new IllegalArgumentException("Percent value must range from 0 to 100");
        }

        mPaint.setColor(color);
        this.percent = percent;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final float width = View.MeasureSpec.getSize(widthMeasureSpec);
        final float adjWidth = (width*((float)percent/100));
        setMeasuredDimension((int)adjWidth, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
    }

}

当我运行这段代码时,我得到以下错误

 01-11 10:51:10.402 26974-26974/com.example.graphs.viewchart E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.graphs.viewchart, PID: 26974
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.graphs.viewchart/com.example.graphs.viewchart.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class com.github.ppartisan.graphish.view.BarView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
    at android.app.ActivityThread.access0(ActivityThread.java:168)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5753)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
    Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class com.github.ppartisan.graphish.view.BarView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.example.graphs.viewchart.MainActivity.onCreate(MainActivity.java:21)
    at android.app.Activity.performCreate(Activity.java:6112)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
    at android.app.ActivityThread.access0(ActivityThread.java:168) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5753) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.github.ppartisan.graphish.view.BarView" on path: DexPathList[[zip file "/data/app/com.example.graphs.viewchart-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.view.LayoutInflater.createView(LayoutInflater.java:571)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at com.example.graphs.viewchart.MainActivity.onCreate(MainActivity.java:21) 
    at android.app.Activity.performCreate(Activity.java:6112) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
    at android.app.ActivityThread.access0(ActivityThread.java:168) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5753) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
    Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.github.ppartisan.graphish.view.BarView" on path: DexPathList[[dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-support-annotations-24.2.1_ee83e15951f6e098dcf8d5c7289233f29c08ba29-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.exam

是什么导致了这个麻烦? com.github.ppartisan... 来自另一个提供商,我在 Whosebug 中找到了它。 Link -

使用正确的 class 名称。 com.example.graphs.viewchart.BarView

<com.example.graphs.viewchart.BarView
            android:id="@+id/low_bar"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="8dp" />

然后清理-重建-运行