将我创建的类的实例添加到xml文件中

时间:2014-10-16 09:12:29

标签: java android xml

我有一个DrawingView类,该类为其创建一个绘图视图。您可以在下面看到该类的代码。但是,当我想在xml文件中创建该类的实例时,会出现问题。我做错了什么?

xml的代码:

<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="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/new_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/start_new"
        android:src="@drawable/new_pic" />

    <ImageButton
        android:id="@+id/draw_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/brush"
        android:src="@drawable/brush" />

    <ImageButton
        android:id="@+id/erase_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/erase"
        android:src="@drawable/eraser" />

    <ImageButton
        android:id="@+id/save_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/save"
        android:src="@drawable/save" />
</LinearLayout>

<com.example.drawingapp.DrawingView
    android:id="@+id/drawing"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:background="#FFFFFFFF" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <!-- Top Row -->

    <LinearLayout
        android:id="@+id/paint_colors"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF660000"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF660000" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FFFF0000"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FFFF0000" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FFFF6600"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FFFF6600" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FFFFCC00"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FFFFCC00" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF009900"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF009900" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF009999"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF009999" />
    </LinearLayout>
    <!-- Bottom Row -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:layout_margin="2dp"
            android:background="#FF0000FF"
            android:contentDescription="@string/paint"
            android:onClick="paintClicked"
            android:src="@drawable/paint"
            android:tag="#FF0000FF" />

        <ImageButton
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF990099"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF990099" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF6666"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF6666" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFFFFFF"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFFFFFF" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF787878"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF787878" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF000000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF000000" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

DrawingView类代码:

package com.example.drawingapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.TypedValue;

public class DrawingView extends View {
    // drawing path
    private Path drawPath;
    // drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    // initial color
    private int paintColor = 0xFF660000;
    // canvas
    private Canvas drawCanvas;
    // canvas bitmap
    private Bitmap canvasBitmap;
    private ImageButton currPaint;
    private DrawingView drawView;
    private boolean erase = false;

    private float brushSize, lastBrushSize;

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
        drawView = (DrawingView) findViewById(R.id.drawing);
        LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
        currPaint = (ImageButton) paintLayout.getChildAt(0);
        currPaint.setImageDrawable(getResources().getDrawable(
                R.drawable.paint_pressed));

    }

    private void setupDrawing() {
        // get drawing area setup for interacion
        drawPath = new Path();
        drawPaint = new Paint();

        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(20);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;
        drawPaint.setStrokeWidth(brushSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        float touchX = event.getX();
        float touchY = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;
        default:
            return false;
        }
        invalidate();
        return true;
    }

    public void paintClicked(View view) {
        if (view != currPaint) {
            // update color
            drawView.setErase(false);
            drawView.setBrushSize(drawView.getLastBrushSize());
            ImageButton imgView = (ImageButton) view;
            String color = view.getTag().toString();
            drawView.setColor(color);
            imgView.setImageDrawable(getResources().getDrawable(
                    R.drawable.paint_pressed));
            currPaint.setImageDrawable(getResources().getDrawable(
                    R.drawable.paint));
            currPaint = (ImageButton) view;
        }
    }

    public void setColor(String newColor) {
        // set color
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);

    }

    public void setBrushSize(float newSize) {
        // update size
        float pixelAmount = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, newSize, getResources()
                        .getDisplayMetrics());
        brushSize = pixelAmount;
        drawPaint.setStrokeWidth(brushSize);
    }

    public void setLastBrushSize(float lastSize) {
        lastBrushSize = lastSize;
    }

    public float getLastBrushSize() {
        return lastBrushSize;
    }

    public void setErase(boolean isErase) {
        // set erase true or false
        erase = isErase;
        if (erase)
            drawPaint
                    .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        else
            drawPaint.setXfermode(null);
    }

    public void startNew() {
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }
}

Logcat向我显示错误:

   10-16 11:57:56.171: E/AndroidRuntime(17594): FATAL EXCEPTION: main
10-16 11:57:56.171: E/AndroidRuntime(17594): Process: com.example.drawingapp, PID: 17594
10-16 11:57:56.171: E/AndroidRuntime(17594): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawingapp/com.example.drawingapp.MainActivity}: android.view.InflateException: Binary XML file line #44: Error inflating class com.example.drawingapp.DrawingView.java
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.os.Looper.loop(Looper.java:136)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.reflect.Method.invoke(Method.java:515)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at dalvik.system.NativeStart.main(Native Method)
10-16 11:57:56.171: E/AndroidRuntime(17594): Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class com.example.drawingapp.DrawingView.java
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.Activity.setContentView(Activity.java:1929)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at com.example.drawingapp.MainActivity.onCreate(MainActivity.java:28)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.Activity.performCreate(Activity.java:5231)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-16 11:57:56.171: E/AndroidRuntime(17594):    ... 11 more
10-16 11:57:56.171: E/AndroidRuntime(17594): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.drawingapp.DrawingView.java" on path: DexPathList[[zip file "/data/app/com.example.drawingapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.drawingapp-1, /vendor/lib, /system/lib]]
10-16 11:57:56.171: E/AndroidRuntime(17594):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.createView(LayoutInflater.java:559)
10-16 11:57:56.171: E/AndroidRuntime(17594):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
10-16 11:57:56.171: E/AndroidRuntime(17594):    ... 24 more

2 个答案:

答案 0 :(得分:0)

你也可能在LogCat中看到NullPointerException。

findViewById(R.id.paint_colors)将返回null,因为没有具有该ID的子视图。实际上,View类不能有任何子视图。

我怀疑你的意思是要将一个布局扩展到视图中,在这种情况下你应该扩展ViewGroup而不是View并在super(context, attrs)下包含以下几行:

LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_layout_name, this);

答案 1 :(得分:0)

为什么你在构造函数中调用了这个:

drawView = (DrawingView) findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);

您已经在 DrawingView 中,因此无法使用绘图 ID找到 DrawingView 。对于 paintLayout ,它是相同的,您的DrawingView没有具有id paint_colors 的LinearLayout。您的 DrawingView 只是查看而不是 ViewGroup

我认为你在这一行有一个NullPointerException:

currPaint = (ImageButton) paintLayout.getChildAt(0);

在此行测试之前,如果您的paintLayout为null,请