所以,我在java中的某种菜鸟,我正在尝试为Android制作应用程序。 我得到一些错误,但我无法解决它们。任何人都可以帮我找到问题所在吗?
这是我的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;
}
}
我的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.
答案 0 :(得分: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部分:
好的,我现在将描述每个错误。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
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
用这个
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
这个错误有时候很难解决,有时你只需添加final,有时你需要变量的最终副本,有时你需要真正重构你的代码。幸运的是,这次添加最终会做。
打开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 { 公共Ponto2D inicio; 公共Ponto2D fim; public int cor; // ... }
Reta.inicio字段不可见
看,这就是你使用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()); this.inicio = p1; this.fim = p2; //让我们使用这些而不是新对象。除非你想要新的物品。 } }
这种代码风格将让您了解如何避免这些错误。
最后一部分:
我会帮助你解决这个问题,如果你想用这个来谋生,你必须自己弄明白......你的错误是学生级的初学者编程,而不是真的与Java或Android有关。那些你需要了解并知道如何解决自己的人,因为没有人会浪费时间为你修复未定义的引用,甚至粗暴地提起它(它表明你甚至没有阅读你的代码)
无法对非静态字段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扩展View { 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
)
如果您还有其他问题,请查阅书籍。试试谷歌。谷歌你的错误:)所有这些真的,非常普遍,并已被覆盖数千次。