【问题标题】:Calling a class from a class with parameters (Cannot resolve symbol)从带有参数的类中调用类(无法解析符号)
【发布时间】:2017-08-09 14:42:57
【问题描述】:

我对编程很陌生,所以我可能在这里把整个想法搞错了,但是..

我有两个类,另一个是主类(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);
}
}

【问题讨论】:

  • 你想达到什么目的?
  • 您将Context context 作为参数传递,您不必改为传递this.getApplicationContext() 吗?
  • 感谢@Asew 帮助了我一点。这只是一个测试,但我希望我的绘图方法在一个单独的类中,这样我的 main 将保持干净且易于遵循。当我完成这项工作时,我将使它从您在屏幕上按下的位置到屏幕上标记的最近点画一条线。

标签: java android class


【解决方案1】:

您的代码不在方法中:

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

【讨论】:

  • 感谢您指出这一点,这只是一个简单的错误,但它对参数的工作没有帮助。 Asews 评论说我应该使用this.getApplicationContext() 解决了 DrawingLines 参数的问题。仍然需要弄清楚如何让它在 onDraw 上运行
【解决方案2】:
  1. 首先,如果您想绘制一些东西,您不必以编程方式强制它。 DrawingLines draw = new DrawingLines(Context context); draw.onDraw(canvas)
  2. 将您的视图DrawingLines 放入布局R.layout.activity_main 并创建变量使用findViewById
  3. 然后您可以在该视图上执行drawingLines.invalidate(),因此将重绘该视图(将调用方法void onDraw(Canvas canvas))。

【讨论】:

  • 不确定我的观点是否正确,但这是我添加到 xml 中的内容 <view android:id="@+id/DrawingLines" android:layout_width="match_parent" android:layout_height="match_parent" /> 然后我将其添加到主文件中:View draw = (View) findViewById(R.id.DrawingLines); draw.invalidate(); 如果我使用 DrawingLines.invalidate(); 它给了我一个错误无效是非静态的。这就是我如何让它没有错误,但它仍然在模拟器上崩溃。有什么提示或建议吗?
  • 哦,是的,我将 id 更改为 draw 并将 main 固定为 View draw = (View) findViewById(R.id.draw);
  • 是的,你应该使用DrawingLines view = (DrawingLines) findViewById(R.id.draw),然后当你想要强制重绘时使用view.invalidate()。当您的视图变得可见时,onDraw 会被自动调用,您应该只使用 view.invalidate(),例如,如果您在 DrawingLines 中更改某些内容并想要重绘它。
  • 嗯.. 它仍然不断崩溃。从 Android 监视器读取错误它说:“二进制 XML 文件第 10 行:尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.equals(java.lang.Object)'”xml 上的第 10 行是“
  • 您是否将视图添加到带有孔路径的 XML,例如
【解决方案3】:

我创建了一个可以用手指绘制的简单项目(我将所有执行绘制的人员放在自定义视图中)

绘图视图:

public class DrawView extends View {

    private final static String LOG = "CUSTOM_LOG";

    private Paint paint;
    private Path path = new Path();

    public DrawView(Context context) {
        this(context, null);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Log.d(LOG, "init");
        setOnTouchListener(onTouchListener);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(15);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(LOG, "onDraw");
        canvas.drawPath(path, paint);
    }

    private OnTouchListener onTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            float x = motionEvent.getX();
            float y = motionEvent.getY();
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d(LOG, "ACTION_DOWN");
                    path.moveTo(x, y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.d(LOG, "ACTION_MOVE");
                    path.lineTo(x, y);
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d(LOG, "ACTION_UP");
                    path.reset();
                    break;
            }
            invalidate();
            return true;
        }
    };
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.gitlab.onreg01.simpledrawexample.DrawView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"/>

</FrameLayout>

主活动:

public class MainActivity extends AppCompatActivity {

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

project repository

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2017-03-05
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多