gpt4 book ai didi

java - 从带参数的类调用类(无法解析符号)

转载 作者:行者123 更新时间:2023-12-02 02:40:20 26 4
gpt4 key购买 nike

我对编程还很陌生,所以我的整个想法可能是错误的,但是..

我有两个类,另一个是主类(Main),第二个(DrawingLines)只是普通类。我正在尝试从主类中的 DrawingLines 调用 onDraw 方法。

我做了一些研究,但我发现的只是使用

DrawingLines draw = new DrawingLines ();
draw.onDraw

我认为这应该是这样的,但我只是不知道应该使用哪些参数。您可以在下面的示例中看到我一直尝试使用的参数,但 AndroidStudio 只是给出“无法解析符号”。

主类:

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
final TextView text = (TextView) findViewById(R.id.text);
parent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent ev) {
text.setText("Touch at " + (int)ev.getX() + ", " + (int)ev.getY());
return true;
}
});

}

DrawingLines draw = new DrawingLines(Context context);
draw.onDraw(canvas);

}

绘图线类:

class DrawingLines extends View {

public DrawingLines(Context context) {
super(context);

}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

Rect pallo = new Rect();
pallo.set(0, 0, canvas.getWidth(), canvas.getHeight() / 2);

Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);

canvas.drawRect(pallo, blue);
}
}

最佳答案

您的代码不在方法中:

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
final TextView text = (TextView) findViewById(R.id.text);
parent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent ev) {
text.setText("Touch at " + (int)ev.getX() + ", " + (int)ev.getY());
return true;
}
});

DrawingLines draw = new DrawingLines(this.getApplicationContext());
draw.onDraw(canvas);

}//onCreate

}//class main

关于java - 从带参数的类调用类(无法解析符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45593861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com