0

所以,我是java中的菜鸟,我正在尝试为android制作一个应用程序。我遇到了一些错误,但我无法解决它们。谁能帮我找出问题所在?

这是我的主要活动:

    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;
    }


}

我的 Reta 课程:

    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);

    }
}

我的 DrawView 类:

    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);
        }
    }
}

还有我的 Ponto2D 课程:

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;
    }

}

现在我有(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.

同样在 DrawView.java 的第 34 行,我得到:

- 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.
4

1 回答 1

3

看这个

public class MainActivity extends Activity {

Ponto2D patual = new Ponto2D(); <---- here
//...
}

你从来没有在你的类中定义过 Ponto2D() 构造函数。

添加一个

public Ponto2D()
{
   x = -1;
   y = -1;
}

public Ponto2D(int x, int y)
{
   this();
   this.x = x;
   this.y = y;
}

第2部分:

   public float X=0;
   public float Y=0;

后来你这样做

   Ponto2D patual = new Ponto2D(event.getX(), event.getY());

真的应该是

   Ponto2D patual = new Ponto2D((int)event.getX(), (int)event.getY());

或者你修改你的 Ponto2D 类看起来像这样

public class Ponto2D 
{

float x;
float y;

public Ponto2D ()
{
    x=-1.0;
    y=-1.0;
}

public Ponto2D(float x, float y)
{
    this();
    this.x = x;
    this.y = y;
}
} //end of class

第 3 部分:

好的,我现在将描述每个错误。

  • 构造函数 Ponto2D(float, float) 是未定义的——我们已经介绍了这个。
  • fim 无法解析为变量 - 您从未在 MainActivity 类中定义“fim”。您需要使用定义的值。我猜您想使用 Reta 课程中的“fim”?在这种情况下...

    Reta r1 = 新 Reta (inicio, fim);

将其更改为

Reta r1 = new Reta (new Ponto2D(), new Ponto2D());

或者

Reta r1 = new Reta (new Ponto2D(<your values>), new Ponto2D(<values again>));
  • inicio 无法解析为与前一个相同的变量。

  • 第 47 行:标记“[”上的语法错误,此标记后应为表达式

            DrawView.ar[contador] =rl[];
    
            inicio.X = \.X;
            inicio.Y = fim.Y;
    

这永远不会编译儿子。我不知道你在尝试什么,但我建议你收集你对这个的想法:D

  • 不能在不同方法中定义的内部类中引用非最终变量 rl

这个很有趣。这意味着您不能针对 rl,它是在内部类的外部类中定义的。所以要修复它,只需交换这个

RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);

有了这个

final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);

此错误有时很难修复,有时您只需添加 final,有时您需要变量的最终副本,有时您需要真正重组代码。幸运的是,这次添加 final 就可以了。

  • 字段 DrawView.ar 不可见

打开你的 DrawView 类,看看这个

public class DrawView extends View {
public Paint paint = new Paint();

Reta ar[] = new Reta [10];  //offending line
public int a;
//...

所以要么像这样公开那个领域

public class DrawView extends View
{
    public Paint paint = new Paint();

    public Reta ar[] = new Reta [10];
    //... 
}

或生成 getter/setter。

  • 表达式的类型必须是数组类型,但是
    一旦您对前面提到的类型进行了思考,它就会解析为RelativeLayout,这个类型会自动消失。相信我,我是一个工程师 ;)

  • 字段 Reta.inicio 不可见,这现在是不言自明的......

    公共类 Reta { public Ponto2D inicio; 公共 Ponto2D 电影;公共情报;//... }

  • Reta.inicio 字段不可见

  • 字段 Ponto2D.x 不可见。

看,这就是为什么你使用 getter 和 setter 并且不直接从不同的类访问类的字段的原因。

试着让它像这样

public class Ponto2D 
{
    private int x; //2d plane shouldn't use floating coordinates.
    private int y;

    Ponto2D() { }; //ctor does nothing.

    public Ponto2D(int x, int y)
    {
       this();
       this.x = x;
       this.y = y;
    }

    public int getX() { return x; }
    public int getY() { return y; }
}

public class Reta 
{
    private Ponto2D inicio;
    private Ponto2D fim;
    private int cor;

    Reta()
    {
       fin = new Ponta2D(); //might cause an error because it's not public
       inicio = new Ponta2D(-1, -1); //this should be ok 
    }

    public Reta(Ponto2D p1, Ponto2D p2)
    {

// this.inicio = new Ponto2D(p1.getX(), p1.getY()); //这真是胡说八道 // this.fim = new Ponto2D(p2.getX(), p2.getY()); 这个.inicio = p1; this.fim = p2; //让我们使用这些而不是新对象。除非你想要新的对象。} }

这种代码风格将让您了解如何避免此类错误。

最后部分:

我会帮你解决这个问题,如果你想以此为生,其余的你必须自己弄清楚……你遇到的错误是学生级别的初学者编程错误,与Java 或安卓。那些您需要了解并知道如何解决自己的问题,因为没有人会浪费时间为您修复未定义的引用,而且这样提出它甚至是粗鲁的(这表明您甚至没有阅读您的代码)

无法对非静态字段 DrawView.ar 进行静态引用

好的,这就是你所做的:

public class DrawView extends View {
public Paint paint = new Paint();

Reta ar[] = new Reta [10];
public int a;

所以 DrawView 类有一个名为 ar[] 的成员。

通常,您会像这样访问它:

DrawView dv = new DrawView();
System.out.println(Arrays.toString(dv.ar)); // dv.ar, not DrawView.ar

当您将它作为 DrawView.ar 调用时,这意味着它对于此类的所有实例都是相同的,这就是为什么它说“不能以静态方式引用非静态变量 ar”。

当事物是“静态的”时,它们在该类的所有实例中都是共享的/相同的/通用的。

所以如果你这样做

公共类 DrawView 扩展视图 { public Paint paint = new Paint();

static Reta ar[] = new Reta [10];
public int a;

您的所有 DrawView 实例都将具有相同的 Reta 数组 :) 由于您可能想要多个具有自己的矩形的 drawview,因此它应该是私有的。当它是私有的时,您不能以静态方式 (Class.someMember) 而是从实例 (classInstance.someMember) 或 ((new Class).someMember) 定位它

这意味着....您决定 Reta ar[] 应该是私有的还是公共的。

 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[]; //offending line
            //it should be
            //drawView.ar = something; but thats also wrong because you cannot refer to a non-final variable drawView in a inner-class blahblah so here's what you do
            ((DrawView)v).ar[contador] = rl; //or whatever you want it to be

;)

如果您还有其他问题,请点击书籍。试试谷歌。谷歌你的错误:) 所有这些都是非常非常常见的,并且已经被报道了数千次。

于 2012-10-03T15:08:15.203 回答