SurfaceView错误膨胀布局

时间:2012-02-06 13:49:44

标签: android surfaceview

嗨,我对Android编程比较陌生,但我会感激一些帮助:)

我正在尝试创建一个“浮动”在主活动视图上方的工具栏。该代码实现了一个SurfaceView,用于将项目绘制到其上。在过去的几天里,我一直遇到一些困难,无法让应用程序运行,因为它保持fc! Logcat输出以下错误:

logcat的

02-06 13:19:23.985: E/AndroidRuntime(471): FATAL EXCEPTION: main
02-06 13:19:23.985: E/AndroidRuntime(471): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.odhranlynch.interiordesigner/com.odhranlynch.interiordesigner.NewProduct} android.view.InflateException: Binary XML file line #6: Error inflating class com.odhranlynch.interiordesigner.Panel
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.os.Looper.loop(Looper.java:123)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.ActivityThread.main(ActivityThread.java:3691)
02-06 13:19:23.985: E/AndroidRuntime(471):  at java.lang.reflect.Method.invokeNative(Native Method)
02-06 13:19:23.985: E/AndroidRuntime(471):  at java.lang.reflect.Method.invoke(Method.java:507)
02-06 13:19:23.985: E/AndroidRuntime(471):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
02-06 13:19:23.985: E/AndroidRuntime(471):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
02-06 13:19:23.985: E/AndroidRuntime(471):  at dalvik.system.NativeStart.main(Native Method)
02-06 13:19:23.985: E/AndroidRuntime(471): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.odhranlynch.interiordesigner.Panel
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-06 13:19:23.985: E/AndroidRuntime(471):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.Activity.setContentView(Activity.java:1663)
02-06 13:19:23.985: E/AndroidRuntime(471):  at com.odhranlynch.interiordesigner.NewProduct.onCreate(NewProduct.java:34)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-06 13:19:23.985: E/AndroidRuntime(471):  ... 11 more
02-06 13:19:23.985: E/AndroidRuntime(471): Caused by: java.lang.ClassNotFoundException: com.odhranlynch.interiordesigner.Panel in loader dalvik.system.PathClassLoader[/data/app/com.odhranlynch.interiordesigner-1.apk]
02-06 13:19:23.985: E/AndroidRuntime(471):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-06 13:19:23.985: E/AndroidRuntime(471):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-06 13:19:23.985: E/AndroidRuntime(471):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
02-06 13:19:23.985: E/AndroidRuntime(471):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
02-06 13:19:23.985: E/AndroidRuntime(471):  ... 20 more

我正在使用相对布局,因为我可以将工具栏“捕捉”到底部。 .xml文件如下所示:

new_product.xml

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

<com.odhranlynch.interiordesigner.Panel 
        android:id="@+id/Panel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true" />

<Button android:id="@+id/cut" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Cutout"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

NewProduct.java

package com.odhranlynch.interiordesigner;

import java.util.ArrayList;

import com.odhranlynch.interiordesigner.NewProduct.GraphicObject.Coordinates;
import com.odhranlynch.interiordesigner.R;
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.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.os.Bundle;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class NewProduct extends Activity {

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

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
    private drawThread _thread;
    private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();

    public Panel(Context context, AttributeSet attributes) {
        super(context, attributes);
        getHolder().addCallback(this);
        setFocusable(true);
        _thread = new drawThread(getHolder(), this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (_thread.getSurfaceHolder()) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.circle_point));
                graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
                graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
                _graphics.add(graphic);
            }
            return true;
        }
    }

    @Override 
    public void onDraw(Canvas canvas) {

        Paint paint = new Paint(); 
        canvas.drawColor(Color.YELLOW);
        Bitmap b = Bitmap.createBitmap(200, 200,
        Bitmap.Config.ARGB_8888);

        paint.setFilterBitmap(true);
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.glass_lamp);
        canvas.drawBitmap(bitmapOrg, 0, 0, paint);

        Bitmap bitmap;
        Coordinates coords;
        for (GraphicObject graphic : _graphics) {
            bitmap = graphic.getGraphic();
            coords = graphic.getCoordinates();
            canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
        }


    int width, height;

    Canvas c = new Canvas(b);
    paint.setAlpha(255); //0x80
    c.translate(0, 30);
    c.drawBitmap(bitmapOrg, new Matrix(), paint);
    paint.setColor(Color.BLUE);

    //Create shape using Path
    Path mPath = new Path();
    mPath.moveTo(20, 20);
    mPath.lineTo(100, 200);
    mPath.lineTo(400, 600);
    mPath.lineTo(240, 155);
    mPath.lineTo(250, 175);
    mPath.lineTo(20, 20);
    mPath.close();

    //Cut out a hole
    canvas.clipPath(mPath, Region.Op.XOR);
    //Fill around cut-out with opaque white
    //canvas.drawARGB(200, 255, 0, 0);
    //Restore full canvas clip for any subsequent operations
    //canvas.clipPath(mPath, Region.Op.REPLACE);

    //int h = bitmapOrg.getHeight();
    //canvas.drawBitmap(bitmapOrg, 10, 10, paint);
    //canvas.drawBitmap(b, 0, 10 + h + 10, paint);
    }


     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //TODO something
    }

    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }


    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        } 
    }
}

class drawThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Panel _panel;
    private boolean _run = false;

    public drawThread(SurfaceHolder surfaceHolder, Panel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return _surfaceHolder;
    }

    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {

                    _panel.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

class GraphicObject {
    private Bitmap _bitmap;
    private Coordinates _coordinates;

    public GraphicObject(Bitmap bitmap) {
        _bitmap = bitmap;
        _coordinates = new Coordinates();
    }

    public Bitmap getGraphic() {
        return _bitmap;
    }

    public Coordinates getCoordinates() {
        return _coordinates;
    }

    /**
     * Contains the coordinates of the graphic.
     */
    public class Coordinates {
        private int _x = 100;
        private int _y = 0;

        public int getX() {
            return _x + _bitmap.getWidth() / 2;
        }

        public void setX(int value) {
            _x = value - _bitmap.getWidth() / 2;
        }

        public int getY() {
            return _y + _bitmap.getHeight() / 2;
        }

        public void setY(int value) {
            _y = value - _bitmap.getHeight() / 2;
        }

        public String toString() {
            return "Coordinates: (" + _x + "/" + _y + ")";
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

只需将iternal类Panel,drawThread和GraphicObject移动到单独的文件中即可。这将修复java.lang.ClassNotFoundException,从而解决其他问题。

希望这会有所帮助:)

答案 1 :(得分:0)

如果您在NewProduct.Panel文件中添加<com.odhranlynch.interiordesigner.Panel android:id="@+id/Panel" ... />元素时表示new_product.xml课程,那就错了。

您需要将此课程移至单独的文件,即com/odhranlynch/interiordesigner/Panel.java