我知道这个错误很常见,我也知道我应该添加像
这样的空构造函数public MainActivity(){
super("MainActivity");
但在我的情况下,它不会接受字符串作为参数,所以我真的不知道要传递什么......我将向您展示我的代码,它来自专业Android开发中的Reto Meier示例
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();
}
}
答案 0 :(得分:2)
我不知道你在做什么,但这可能是问题所在:
为什么要命名从查看活动扩展的课程?如果您有MainActivity,通常需要从Activity类扩展它。
问题可能出在那里,你在清单文件中声明你有一个Activity作为MainActivity,但你没有,因为你从View扩展了它。
这是你做的:
public class MainActivity extends Activity{ ... }
public class MyView extends View { /*insert your View code from above*/ }
在MainActivity中你可以尝试
setContentView(new MyView(this));
在AndroidManifest中你声明(我猜你在这里或者喜欢它)
<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>