android版java应用程序中的错误
所以,我有点像java的noob,我正在尝试为android制作一个应用程序。 我有一些错误,但我解决不了。谁能帮我找到问题所在吗 以下是我的主要活动:android版java应用程序中的错误,java,android,compiler-errors,Java,Android,Compiler Errors,所以,我有点像java的noob,我正在尝试为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; im
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;
}
}
我的复读课:
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);
}
}
}
现在我有(44-50)
同样在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.
看看这个
public class MainActivity extends Activity {
Ponto2D patual = new Ponto2D(); <---- here
//...
}
第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
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)
{
第3部分:
好的,我现在将描述每个错误
- 构造函数Ponto2D(float,float)未定义-我们讨论了这个
- fim无法解析为变量-您从未在MainActivity类中定义过“fim”。您需要使用定义的值。我猜你想用你的Reta课程中的“fim”吗?那样的话 Reta r1=新的Reta(inicio,职能指令手册)
Reta r1 = new Reta (new Ponto2D(), new Ponto2D());
或
这永远不会是我的儿子。我不知道你在尝试什么,但我建议你集中你对这一点的想法:D
- 无法在其他方法中定义的内部类中引用非最终变量rl
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
用这个
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
这个错误有时很难修复,有时您只需添加final,有时您需要变量的最终副本,有时您需要真正重新构造代码。幸运的是,这次添加final就可以了
- 字段DrawView.ar不可见
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.ini不可见, 这是不言而喻的现在 公务舱 { 公共庞托迪尼西奥; 公共机构职能指令手册; 公共int cor; //... }
- 字段Reta.ini不可见
- Ponto2D.x字段不可见
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
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.ini=new-Ponto2D(p1.getX(),p1.getY())//这真是胡说八道
//this.fim=new-Ponto2D(p2.getX(),p2.getY());
this.ini=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”
当事物是“静态的”时,它们在该类的所有实例中都是共享的/相同的/公共的
所以如果你这么做
Ponto2D patual = new Ponto2D(event.getX(), event.getY());
公共类DrawView扩展视图{
公共油漆=新油漆()
所有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
)()
如果你还有其他问题,那就看看书。试试谷歌。谷歌你的错误:)所有这些都非常非常普遍,已经被覆盖了数千次。看看这个
public class MainActivity extends Activity {
Ponto2D patual = new Ponto2D(); <---- here
//...
}
第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
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)
{
第3部分:
好的,我现在将描述每个错误
- 构造函数Ponto2D(float,float)未定义-我们讨论了这个
- fim无法解析为变量-您从未在MainActivity类中定义过“fim”。您需要使用已定义的值。我猜您想使用Reta类中的“fim”?在这种情况下 Reta r1=新的Reta(inicio,职能指令手册)
Reta r1 = new Reta (new Ponto2D(), new Ponto2D());
或
我不知道你在尝试什么,但我建议你集中你对这一点的想法:D
- 无法在其他方法中定义的内部类中引用非最终变量rl
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
用这个
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
这个错误有时很难修复,有时您只需要添加final,有时您需要变量的最终副本,有时您需要真正重新构造代码。幸运的是,这次添加final就可以了
- 字段DrawView.ar不可见
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
- 表达式的类型必须是arr