現在我正在研究將在onClick
上畫線的應用程序。我正在LineView.java
班上畫線,並在MainActivity.java
上表演onClick
方法。爲了解決這個問題,我檢查了類似的問題。 第一個解決方案:Android如何從另一個類別調用方法
LineView.onDraw();
它給我這個錯誤:
Multiple markers at this line
- The method onDraw(Canvas) in the type LineView is not applicable for the
arguments()
- Suspicious method call; should probably call "draw" rather than "onDraw"
我也試過在MainActivity寫:
LineView lineView = new LineView(null);
lineView.onDraw();
但它也提供了一個錯誤:
Multiple markers at this line
- The method onDraw(Canvas) in the type LineView is not applicable for the
arguments()
- Suspicious method call; should probably call "draw" rather than "onDraw"
她E公司我LineView.java:
public class LineView extends View {
Paint paint = new Paint();
Point A;
Point B;
boolean draw = false;
public void onCreate(Bundle savedInstanceState) {
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LineView(Context context, AttributeSet attrs, int defstyle) {
super(context, attrs, defstyle);
}
public LineView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
draw = MainActivity.draw;
if(draw){
//A = (getIntent().getParcelableExtra("PointA"));
A = MainActivity.A;
B = MainActivity.B;
canvas.drawLine(A.x, A.y, B.x, B.y, paint);
}
}
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
}
我MainActivity.java
onClick
:
@Override
public void onClick(View v) {
draw = true;
LineView.onDraw();
}
});
提前感謝!
你有沒有將LineView添加到任何佈局或動態初始化LineView? –
是的。我在我的layout_main中有LineView –