简体   繁体   English

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

[英]Binary XML file line #7: Error inflating class

Whenever I try to run my code it crashes but I'm not sure why. 每当我尝试运行我的代码时,它都会崩溃,但我不确定为什么。 Any help? 有什么帮助吗? I basically want to view the squashCourtView and activity_main at the same time. 我基本上想同时查看squashCourtView和activity_main。

This is the exception I'm getting when I run it. 这是我运行它时遇到的异常。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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

This is my activity_main xml file 这是我的activity_main xml文件

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

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



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



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="121dp"
        android:layout_height="386dp"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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>

This is my MainActivity java file 这是我的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;
    }

}

You need to make inner class static: 您需要将内部类设为静态:

public static class SquashCourtView extends SurfaceView implements Runnable

See this question for more details. 有关更多详细信息,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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