编辑:@spiderman感谢您的努力。我通过解决我在上一条评论中所做的事情(关于使用该类作为自定义视图的教程)修复了错误消息,所以我想我会没事的。
Android给了我一个零构造函数错误,所以我现在尝试编写一个默认构造函数,如下所示:
public SignatureActivity() {
super("SignatureActivity");
}
但是我收到一个错误,告诉我我无法将视图对象放入字符串中 - View in View无法应用于java.lang.String。
如何解决这个问题,以便能够成功运行我的代码?
这是班级:
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
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;
public class SignatureActivity extends View {
public SignatureActivity() {
super("SignatureActivity");
}
private Paint paint = new Paint();
private Path path = new Path();
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
public SignatureActivity(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
Log.v("SignatureActivity", "This is onTouchEvent");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Set a new starting point
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
// Connect the points
path.lineTo(eventX, eventY);
break;
default:
return false;
}
// Makes our view repaint and call onDraw
invalidate();
return true;
}
/**@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signature);
} */
/**@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_signature, menu);
return true;
}*/
/**@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
}