我正在使用Android Studio 1.51。
在我的AndroidManifest.xml中,我的PuzzleView活动中出现错误,并显示以下错误:

PuzzleView没有默认的构造函数
不能将PuzzleView分配给android.app.Activity。

这是我的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mycanvas.example.com.mycanvas"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyCanvas"
            android:label="@string/title_activity_my_canvas" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PuzzleView"
            android:label="@string/title_activity_my_canvas" >
        </activity>
    </application>
</manifest>


这是我的PuzzleView.java

    package mycanvas.example.com.mycanvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;

public class PuzzleView extends View {
    private static final String TAG = "MyCanvas";
    //private final Game game;
    private float width;   //width of one tile
    private float height;  //height of one tile
    private int selX;     // X index of selection
    private int selY;     // Y index of selection
    private final Rect selRect = new Rect();


    public PuzzleView(Context context, AttributeSet attrs) {
        super(context,attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    public PuzzleView(Context context) {
        super(context);
        //this.game = (Game) context;
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    public PuzzleView(Context context, AttributeSet attrs, int defStyle) {
        super(context,attrs,defStyle);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        width = w / 9f;
        height = h / 9f;
        getRect(selX, selY, selRect);
        Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void getRect(int x, int y, Rect rect) {
        rect.set((int)(x * width), (int)(y * height), (int) (x * width + width), (int) (y * height + height));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //Draw the background...
        Paint background = new Paint();
        background.setColor(getResources().getColor(R.color.puzzle_background));
        canvas.drawRect(0, 0, getWidth(), getHeight(), background);

        // Draw the board...
        // Define colors for the grid lines
        Paint dark = new Paint();
        dark.setColor(getResources().getColor(R.color.puzzle_dark));

        Paint hilite = new Paint();
        hilite.setColor(getResources().getColor(R.color.puzzle_hilite));

        Paint light = new Paint();
        light.setColor(getResources().getColor(R.color.puzzle_light));

        // Draw the minor grid lines
        for (int i = 0; i < 9; i++) {
            canvas.drawLine(0, i * height, getWidth(), i * height, light);
            canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
            canvas.drawLine(i * width, 0, i * width, getHeight(), light);
            canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), hilite);
        }

        // Draw the major grid lines
        for (int i = 0; i < 9; i++) {
            if ( i % 3 != 0)
            continue;
            canvas.drawLine(0, i * height, getWidth(), i * height, dark);
            canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
            canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
            canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), hilite);
        }

        // Draw the numbers...
        // Define color and style for numbers
        Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
        foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
        foreground.setStyle(Style.FILL);
        foreground.setTextSize(height * 0.75f);
        foreground.setTextScaleX(width / height);
        foreground.setTextAlign(Paint.Align.CENTER);

        // Draw the number in the center of the tile
        FontMetrics fm = foreground.getFontMetrics();
        //Centering in X; use alignment (and X at midpoint)
        float x = width / 2;
        //Centering in Y; measure ascent/descent first
        float y = height / 2 - (fm.ascent + fm.descent) / 2;
        for (int i = 0; i < 9; i++) {
            for(int j = 0; j<9; j++) {
            //canvas.drawText(this.game.getTileString(i, j), i * width + x, j * height + y, foreground);
            canvas.drawText("X", i * width + x, j * height + y, foreground);
            }
        }

        // Draw the hints...
        //Pick a hint color based on #moves left
    //         Paint hint = new Paint();
    //         int c[] = { getResources().getColor(R.color.puzzle_hint_0),
    //                         getResources().getColor(R.color.puzzle_hint_1),
    //                         getResources().getColor(R.color.puzzle_hint_1), };
    //         Rect r = new Rect();
    //         for (int i = 0; i<9; i++) {
    //                 for (int j=0; j<9;j++) {
    //                         int movesleft = 9; //9 - game.getUsedTiles(i, j).length;
    //                         if(movesleft < c.length) {
    //                                 getRect(i, j, r);
    //                                 hint.setColor(c[movesleft]);
    //                                 canvas.drawRect(r, hint);
    //                         }
    //                 }
    //         }

        // Draw the selection...
        Log.d(TAG, "selRect=" + selRect);
        Paint selected = new Paint();
        selected.setColor(getResources().getColor(R.color.puzzle_selected));
        canvas.drawRect(selRect, selected);
    }

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event) {
        Log.d(TAG, "onKeyDown: keyCode=" + keyCode + ", event=" + event);
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_UP:
            select(selX, selY -1);
            break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
            select(selX, selY + 1);
            break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
            select(selX - 1, selY);
            break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            select(selX + 1, selY);
            break;
            case KeyEvent.KEYCODE_0:
            case KeyEvent.KEYCODE_SPACE:  setSelectedTile(0); break;
            case KeyEvent.KEYCODE_1:  setSelectedTile(1); break;
            case KeyEvent.KEYCODE_2:  setSelectedTile(2); break;
            case KeyEvent.KEYCODE_3:  setSelectedTile(3); break;
            case KeyEvent.KEYCODE_4:  setSelectedTile(4); break;
            case KeyEvent.KEYCODE_5:  setSelectedTile(5); break;
            case KeyEvent.KEYCODE_6:  setSelectedTile(6); break;
            case KeyEvent.KEYCODE_7:  setSelectedTile(7); break;
            case KeyEvent.KEYCODE_8:  setSelectedTile(8); break;
            case KeyEvent.KEYCODE_9:  setSelectedTile(9); break;
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_DPAD_CENTER:
            //game.showKeypadOrError(selX, selY);
            break;
            default:
            return super.onKeyDown(keyCode, event);
        }
        return true;
    }

    private void select(int x, int y) {
        invalidate(selRect);
        selX = Math.min(Math.max(x, 0), 8);
        selY = Math.min(Math.max(y, 0), 8);
        getRect(selX, selY, selRect);
        invalidate(selRect);
    }

//       @Override
//       public boolean onTouchEvent(MotionEvent event){
//         if (event.getAction() != MotionEvent.ACTION_DOWN)
//                 return super.onTouchEvent(event);
//
//         select((int) (event.getX() / width), (int)(event.getY() / height));
//         //game.showKeypadOrError(selX, selY);
//         Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
//         return true;
//       }
//
    public void setSelectedTile(int tile) {
        //if (game.setTileIfValid(selX, selY, tile)) {
        //   invalidate(); //may change hints
        //} else {
        // Number is not valid for this tile
        Log.d(TAG, "setSelectedTile: invalid: " + tile);
        //startAnimation(AnimationUtils.loadAnimation(game, R.anim.shake));
        //}
    }
}


这是MyCanvas.java:

package mycanvas.example.com.mycanvas;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Rect;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MyCanvas extends Activity {

    private static final String TAG = "MyCanvas";
    private PuzzleView puzzleView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new GraphicsView(this));

        puzzleView = new PuzzleView(this);
        setContentView(puzzleView);
        puzzleView.requestFocus();
    }

    static public class GraphicsView extends View {

        private Path circle = new Path();
        private final Rect rect = new Rect(); //Circle circle = new Circle();
        //private final Circel circle = new Circ();
        private Paint cPaint, tPaint, myPaint, background;
        private int width = 0, height = 0;

        private static final String QUOTE = "Now is the time for all " +
            "good men to come to the aid of their country.";

        private static final String QUOTE2 = "This is just a teaser of the kind " +
            "of graphics ANDROID can display. Keep exploring the Graphics API.";

        public GraphicsView(Context context) {
            super(context);
            background = new Paint();
            background.setColor(Color.YELLOW);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // Drawing commands go here

            Log.i(TAG, "GraphicsView - onDraw()");
            //background.setColor(getResources().getColor(R.color.puzzle_background));
            rect.set(10, 10, 210, 210);

            //cPaint.setColor(Color.LTGRAY);
            //circle.addCircle(150, 150, 100, Direction.CW);

            //canvas.drawPath(circle, cPaint);
            myPaint = new Paint();
            myPaint.setColor(Color.rgb(0, 0, 0));
            myPaint.setStrokeWidth(10);

            cPaint = new Paint();
            cPaint.setColor(Color.YELLOW);
            cPaint.setStrokeWidth(10);

            tPaint = new Paint();
            tPaint.setColor(Color.BLACK);
            tPaint.setStrokeWidth(10);

            canvas.drawRect(rect, myPaint);

            //draw a circle on the CANVAS
            canvas.drawCircle(150, 150,  30, cPaint);

            //add a circle to the PATH
            circle.addCircle(250,  250,  100,  Direction.CW);
            canvas.drawPath(circle, cPaint);
            canvas.drawTextOnPath(QUOTE2,  circle,  0,  20,  tPaint);

            canvas.rotate(30);
            canvas.drawText("Watch the ANDROID CANVAS !!!!", 300, 300, tPaint);

            //canvas.rotate(120);
            //canvas.drawText("Watch the ANDROID CANVAS !!!!", 450, 450, tPaint);
            //canvas.drawRect(0,  0, getWidth(), getHeight(), background);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

最佳答案

您的Puzzleview扩展视图不是活动。.MyCanvas扩展Activity类..您需要将MyCanvas添加到清单中而不是PuzzleView。

关于android - AndroidManifest.xml错误:无法将 View 分配给 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35823151/

10-10 07:00