简体   繁体   中英

java.lang.InstantiationException: can't instantiate class; no empty constructor

I know this error is pretty common and I also know that I should just add the empty constructor like

public MainActivity(){
super("MainActivity");

but in my case it won't accept a string as an argument, so I don't really know what to pass... I'll show you my code which is taken from a Reto Meier example in Professional Android Development.

package com.virtualflyer.compass;

import...

public class MainActivity extends View {

    public MainActivity(Context context){
        super(context);
        initCompassView();
    }

    public MainActivity(Context context, AttributeSet attrs){
        super (context, attrs);
        initCompassView();
    }

    public MainActivity(Context context,AttributeSet attrs,int defaultstyle){
        super (context, attrs,defaultstyle);
        initCompassView();
    }

    private Paint markerPaint;
    private Paint textPaint;
    private Paint circlePaint;
    private String northString;
    private String southString;
    private String westString;
    private String eastString;
    private int textHeight;

    protected void initCompassView(){
        setFocusable(true);

        Resources r=this.getResources();

        circlePaint=new Paint (Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(r.getColor(R.color.background_color));
        circlePaint.setStrokeWidth(1);
        circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);

        northString=r.getString(R.string.cardinal_north);
        southString=r.getString(R.string.cardinal_south);
        eastString=r.getString(R.string.cardinal_east);
        westString=r.getString(R.string.cardinal_west);

        textPaint= new Paint (Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(r.getColor(R.color.text_color));

        textHeight=(int)textPaint.measureText("yY");
        markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        markerPaint.setColor(r.getColor(R.color.marker_color));



    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        int measuredWidth=measure(widthMeasureSpec);
        int measuredHeight=measure(heightMeasureSpec);
        int d =Math.min(measuredWidth,measuredHeight);
        setMeasuredDimension(d,d);
    }

    private int measure(int measureSpec){
        int result=0;
        int specMode=MeasureSpec.getMode(measureSpec);
        int specSize=MeasureSpec.getSize(measureSpec);
        if (specMode==MeasureSpec.UNSPECIFIED){
            result=200;
        } else {
            result=specSize;
        }
        return result;
    }

    private float bearing;

    public void setBearing (float _bearing){
        bearing=_bearing;
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
    }

    public float getBearing(){
        return bearing;
    }

    @Override
    public boolean dispatchPopulateAccessibilityEvent(final AccessibilityEvent event){
        super.dispatchPopulateAccessibilityEvent(event);
        if (isShown()){
            String bearingStr= String.valueOf(bearing);
            if (bearingStr.length()>AccessibilityEvent.MAX_TEXT_LENGTH)
                bearingStr=bearingStr.substring(0,AccessibilityEvent.MAX_TEXT_LENGTH);
            event.getText().add(bearingStr);
            return true;
        }
        else{
            return false;
        }

        }


    @Override
    protected void onDraw(Canvas canvas){
        int mMeasuredHeight=getMeasuredHeight();
        int mMeasuredWidth=getMeasuredWidth();
        int px= mMeasuredWidth/2;
        int py=mMeasuredHeight/2;

        int radius=Math.min(px,py);

        canvas.drawCircle(px,py,radius,circlePaint);
        canvas.save();
        canvas.rotate(-bearing,px,py);

        int textWidth=(int)textPaint.measureText("W");
        int cardinalX=px-textWidth/2;
        int cardinalY=py-radius+textHeight;

        for (int i=0; i<24; i++){
            canvas.drawLine(px,py-radius,px,py-radius+10,markerPaint);
            canvas.save();
            canvas.translate(0,textHeight);

            if (i%6==0){
                String dirString ="";
                switch (i){
                    case(0) :{
                        dirString=northString;
                        int arrowY=2*textHeight;
                        canvas.drawLine(px,arrowY,px-5,3*textHeight,markerPaint);
                        break;
                    }
                    case(6) :dirString=eastString;break;
                    case(12) :dirString=southString;break;
                    case(18) :dirString=westString;break;
                }
                canvas.drawText(dirString, cardinalX,cardinalY,textPaint);
            }
            else if (i%3==0){
                String angle =String.valueOf(i*15);
                float angleTextWidth=textPaint.measureText(angle);

                int angleTextX=(int) (px-angleTextWidth/2);
                int angleTextY=(int) py-radius+textHeight;
                canvas.drawText(angle,angleTextX,angleTextY,textPaint);
            }
            canvas.restore();
            canvas.rotate(15,px,py);
        }
        canvas.restore();
    }



}

I don't know what you are doing, but this might be the problem:

  1. Why are you naming your class extended from View an Activity? If you have a MainActivity, you generally want to extend that from Activity class.

  2. The problem might be there, that you declare in your manifest file that you have an Activity as MainActivity, but you don't because you extended that from View.

Here's what you do:

public class MainActivity extends Activity{ ... }
public class MyView extends View { /*insert your View code from above*/ }

and in MainActivity you might try

setContentView(new MyView(this));

And in the AndroidManifest you declare (I guess you had the same here or like it)

    <activity
        android:name=".MainActivity"
        android:label="MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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