二进制 XML 文件第 7 行:膨胀类时出错

Posted

技术标签:

【中文标题】二进制 XML 文件第 7 行:膨胀类时出错【英文标题】:Binary XML file line #7: Error inflating class 【发布时间】:2016-03-05 18:03:30 【问题描述】:

每当我尝试运行我的代码时,它都会崩溃,但我不知道为什么。有什么帮助吗?我基本上是想同时查看squashCourtView和activity_main。

这是我运行时遇到的异常。

java.lang.RuntimeException: Unable to start activity ComponentInfocom.wolfy.incrementalchicken/com.wolfy.incrementalchicken.MainActivity: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.wolfy.incrementalchicken.MainActivitySquashCourtView

这是我的 activity_main xml 文件

    <LinearLayout
        android:layout_
        android:layout_>

        <view class= "com.wolfy.incrementalchicken.MainActivity$SquashCourtView"
            android:layout_
            android:layout_
            android:id="@+id/squashCourtView" />
    </LinearLayout>



    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        tools:context="com.wolfy.incrementalchicken.MainActivity"
        android:background="@drawable/backgrounddd"
        android:layout_gravity="center_horizontal|bottom">



    <TextView
        android:layout_
        android:layout_
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textEggs"
        android:background="#7d3fa124"
        android:layout_marginTop="0dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:contentDescription="eggs"
        android:layout_
        android:layout_
        android:id="@+id/imageEggs"
        android:src="@drawable/eggs"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="false"
        android:layout_marginBottom="422dp"
        android:layout_marginTop="-20dp"
        android:layout_marginEnd="280dp" />

    <ImageView
        android:contentDescription="upgrade"
        android:layout_
        android:layout_
        android:id="@+id/imageUpgrade"
        android:src="@drawable/upgrade"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="280dp"
        android:layout_marginBottom="422dp"
        android:layout_marginTop="-20dp" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/imageView"
        android:layout_marginBottom="546dp"
        android:src="@drawable/rope"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

</LinearLayout >

</FrameLayout>

这是我的 MainActivity java 文件

package com.wolfy.incrementalchicken;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity /*implements View.OnTouchListener*/

    Canvas canvas;
    SquashCourtView squashCourtView;

    //Used for getting display details like the number of pixels
    Display display;
    Point size;
    int screenWidth;
    int screenHeight;

    Point ballPosition;
    int ballWidth;

    boolean ballIsMovingDown;

    //stats
    long lastFrameTime;
    int fps;
    int score;
    int lives;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        squashCourtView = (SquashCourtView) findViewById(R.id.squashCourtView);

        //Get the screen size in pixels
        display = getWindowManager().getDefaultDisplay();
        size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) 
            display.getSize(size);
        
        screenWidth = size.x;
        screenHeight = size.y;

        ballWidth = screenWidth / 35;
        ballPosition = new Point();
        ballPosition.x = screenWidth / 2;
        ballPosition.y = 1 + ballWidth;

        lives = 3;


        /*setContentView(R.layout.activity_main);

        final ImageView imageEggs = (ImageView)findViewById(R.id.imageEggs);
        final ImageView imageUpgrade = (ImageView)findViewById(R.id.imageUpgrade);
        TextView test = (TextView)findViewById(R.id.textEggs);
        imageUpgrade.setOnTouchListener(this);*/
    


    /*@Override
    public boolean onTouch(View v, MotionEvent event) 
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
            final int x = (int) event.getX();
            final int y = (int) event.getY();

            //now map the coords we got to the
            //bitmap (because of scaling)
            ImageView imageView = ((ImageView)v);
            Bitmap bitmap =((BitmapDrawable)imageView.getDrawable()).getBitmap();
            int pixel = bitmap.getPixel(x,y);

            //now check alpha for transparency
            int alpha = Color.alpha(pixel);
            if (alpha != 0) 
                //do whatever you would have done for your click event here
                Intent i;
                i = new Intent(this, StructureActivity.class);
                startActivity(i);
            
        
        return true; //we've handled the event
    */


    class SquashCourtView extends SurfaceView implements Runnable 
        Thread ourThread = null;
        SurfaceHolder ourHolder;
        volatile boolean playingSquash;
        Paint paint;

        public SquashCourtView(Context context) 
            super(context);
            ourHolder = getHolder();
            paint = new Paint();
            ballIsMovingDown = true;

        

        @Override
        public void run() 
            while (playingSquash) 
                updateCourt();
                drawCourt();
                controlFPS();

            

        


        public void updateCourt() 


            //depending upon the two directions we should be
            //moving in adjust our x any positions
            if (ballIsMovingDown) 
                ballPosition.y += 12;
            

            //if hits bottom
            if (ballPosition.y > screenHeight - 4*ballWidth) 
                ballIsMovingDown = false;


            

        

        public void drawCourt() 

            if (ourHolder.getSurface().isValid()) 
                canvas = ourHolder.lockCanvas();
                //Paint paint = new Paint();
                canvas.drawColor(Color.BLACK);//the background
                paint.setColor(Color.argb(255, 255, 255, 255));
                paint.setTextSize(45);
                canvas.drawText("Score:" + score + " Lives:" + lives + " fps:" + fps, 20, 40, paint);



                Bitmap bMapEgg = BitmapFactory.decodeResource(getResources(), R.drawable.egg);
                bMapEgg = scaleDown(bMapEgg,140,true);

                canvas.drawBitmap(bMapEgg, ballPosition.x, ballPosition.y, paint);





                ourHolder.unlockCanvasAndPost(canvas);
            

        

        public void controlFPS() 
            long timeThisFrame = (System.currentTimeMillis() - lastFrameTime);
            long timeToSleep = 15 - timeThisFrame;
            if (timeThisFrame > 0) 
                fps = (int) (1000 / timeThisFrame);
            
            if (timeToSleep > 0) 

                try 
                    ourThread.sleep(timeToSleep);
                 catch (InterruptedException e) 
                

            

            lastFrameTime = System.currentTimeMillis();
        


        public void pause() 
            playingSquash = false;
            try 
                ourThread.join();
             catch (InterruptedException e) 
            

        

        public void resume() 
            playingSquash = true;
            ourThread = new Thread(this);
            ourThread.start();
        



    

    @Override
    protected void onStop() 
        super.onStop();

        while (true) 
            squashCourtView.pause();
            break;
        

        finish();
    


    @Override
    protected void onResume() 
        super.onResume();
        squashCourtView.resume();
    

    @Override
    protected void onPause() 
        super.onPause();
        squashCourtView.pause();
    

    public boolean onKeyDown(int keyCode, KeyEvent event) 
        if (keyCode == KeyEvent.KEYCODE_BACK) 
            squashCourtView.pause();
            finish();
            return true;
        
        return false;
    

    public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
                                   boolean filter) 
        float ratio = Math.min(
                (float) maxImageSize / realImage.getWidth(),
                (float) maxImageSize / realImage.getHeight());
        int width = Math.round((float) ratio * realImage.getWidth());
        int height = Math.round((float) ratio * realImage.getHeight());

        Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
                height, filter);
        return newBitmap;
    


【问题讨论】:

【参考方案1】:

您需要将内部类设为静态:

public static class SquashCourtView extends SurfaceView implements Runnable

更多详情请见this question。

【讨论】:

以上是关于二进制 XML 文件第 7 行:膨胀类时出错的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 13 行:在扩展 VideoView 的类上膨胀类时出错

在 Cardview 中膨胀二进制 xml 文件的类时出错

二进制 XML 文件第 28 行:二进制 XML 文件第 28 行:膨胀类 <unknown> 时出错

膨胀类 ImageView 时出错 - android.view.InflateException:二进制 XML 文件第 10 行:膨胀类 ImageView 时出错

android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错

二进制 XML 文件第 13 行:膨胀类片段时出错