So, Im some kind a noob in java and I’m trying to make a app for android.
Î’m getting some errors but I cant solve them. Can anyone help me finding whatthe problem is ?
Here is my mainActivity:
package com.example.w;
//import com.example.jkhgvcxz.R;
import views.DrawView;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
Ponto2D patual = new Ponto2D();
int contador = 0;
DrawView drawView;
View view2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500, 500);
params.leftMargin = 50;
params.topMargin = 50;
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
rl.addView(drawView, params);
drawView.setOnTouchListener(new View.OnTouchListener() {
// @Override
public boolean onTouch(View v, MotionEvent event) {
Ponto2D patual = new Ponto2D((int)event.getX(), (int)event.getY()); Reta r1 = new Reta (inicio, fim);
DrawView.ar[contador] =rl[];
inicio.X = \.X;
inicio.Y = fim.Y;
contador++;
if (event.getAction()== MotionEvent.ACTION_DOWN)
{
drawView.a=7;
drawView.antigox=drawView.X;
drawView.antigoy=drawView.Y;
drawView.X=event.getX();
drawView.Y=event.getY();
//drawView.draw();
drawView.desenhar_quadrado();}
return true;
}
});
}
public void btn(View v)
{
drawView.desenhar_quadrado();
}
public void btn_verde(View v)
{
drawView.paint.setColor(Color.GREEN);
}
public void btn_vermelho(View v)
{
drawView.paint.setColor(Color.RED);
}
public void btn_preto(View v)
{
drawView.paint.setColor(Color.BLACK);
}
public void btn_azul(View v)
{
drawView.paint.setColor(Color.BLUE);
}
public void btn_amarelo(View v)
{
drawView.paint.setColor(Color.YELLOW);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My Reta class:
package com.example.w;
public class Reta {
Ponto2D inicio;
Ponto2D fim;
int cor;
public Reta(Ponto2D p1, Ponto2D p2){
inicio = new Ponto2D(p1.x, p1.y);
fim = new Ponto2D(p2.x, p2.y);
}
}
My DrawView class:
package views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import com.example.w.Reta;
public class DrawView extends View {
public Paint paint = new Paint();
Reta ar[] = new Reta [10];
public int a;
public float antigox=0;
public float antigoy=0;
public float X=0;
public float Y=0;
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
a=0;
}
public void desenhar_quadrado()
{
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
for(int j=0; j<10; j++){
drawLine(ar[j].inicio.x, ar[j].inicio.y);
}
}
}
And my Ponto2D class:
package com.example.w;
public class Ponto2D {
int x;
int y;
public Ponto2D()
{
x = -1;
y = -1;
}
public Ponto2D(int x, int y)
{
this();
this.x = x;
this.y = y;
}
}
Now i have(44-50)
- The constructor Ponto2D(float, float) is undefined
- fim cannot be resolved to a variable
- inicio cannot be resolved to a variable
- line 47: Syntax error on token "[", Expression expected after this token
- Cannot refer to a non-final variable rl inside an inner class defined in a different method
- The field DrawView.ar is not visible
- The type of the expression must be an array type but it resolved to RelativeLayout
- fim cannot be resolved to a variable - inicio cannot be resolved to a variable.
Also in DrawView.java, line 34 I get:
- The field Reta.inicio is not visible,
- The method drawLine(int, int) is undefined for the type DrawView
- The field Reta.inicio is not visible
- The field Ponto2D.x is not visible.
Look at this
you never defined a Ponto2D() constructor in your class.
Add a
Part 2:
and later you do this
which should really be
or you rework your Ponto2D class to look like this
Part 3:
ok, i’ll describe each error now.
fim cannot be resolved to a variable – you never defined ‘fim’ in your MainActivity class. you need to use defined values. I’m guessing you wanted to use the ‘fim’ from your Reta class? in that case…
Reta r1 = new Reta (inicio, fim);
change this to
or
inicio cannot be resolved to a variable
same thing as previous one.
line 47: Syntax error on token “[“, Expression expected after this token
this will never compile son. I don’t know what you’re trying but I suggest you gather your thoughts on this one 😀
this one is interesting. this means that you cannot target rl, which is defined in a outer class from an inner class. So to fix it, just swap this
with this
this error can sometimes be tricky to fix, sometimes you just add final, sometimes you need a final copy of your variable and some other times you need to really restructure your code. fortunately, this time adding final will do.
open your DrawView class and see this
so either make that field public like
or generate getters/setters.
The type of the expression must be an array type but it resolved to RelativeLayout
once you gather your thoughts on the beforementioned one, this one will automagically disappear. trust me, i’m an engineer 😉
The field Reta.inicio is not visible,
this is self-explanatory by now…
public class Reta
{
public Ponto2D inicio;
public Ponto2D fim;
public int cor;
//…
}
The field Reta.inicio is not visible
see, this is why you use getters and setters and don’t access the class’s fields directly from different classes.
Try to make it like this
// this.inicio = new Ponto2D(p1.getX(), p1.getY()); //this is bullshit really
// this.fim = new Ponto2D(p2.getX(), p2.getY());
this.inicio = p1;
this.fim = p2; //lets use these instead of new objects. unless you want new objects.
}
}
this code-style will give you an idea how to avoid mistakes like these.
last part:
I’ll help you with this one, the rest you will have to figure out on your own if you ever want to make a living out of this… the errors you have are student-level beginner programming ones, not really tied to Java or Android. Those you need to understand and know how to resolve yourself because noone will waste time fixing undefined references for you, and it is even rude to bring it up like that (it shows you didn’t even read your code)
Cannot make a static reference to the non-static field DrawView.ar
Ok, so here’s waht you did:
so DrawView class has a member named ar[].
Normally, you would access it like this:
when you invoke it as DrawView.ar that means it is the same for all instances of this class
which is why it says “cannot refer to non-static variable ar in a static manner”.
When things are ‘static’ they are shared/same/common throughout all instances of that class.
So if you do this
public class DrawView extends View {
public Paint paint = new Paint();
all your DrawView instances will have the same Reta array 🙂 Since you progbably want more than one drawview with it’s own rectangles, it should be private. And when it’s private, you cannot target it in a static manner (Class.someMember) but from an instance (classInstance.someMember) or ((new Class).someMember)
which means…. you decide whether or not Reta ar[] should be private or public.
😉
If you have further questions, hit the books. Try google. Google your errors 🙂 all of those are really, really common and have been covered thousands of times.