为什么我在此代码中出现对象分辨率错误?关于'displayObject.ob1'的for循环中发生错误:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View v) {
int resId;
switch (v.getId()) {
case R.id.button1:
resId = R.raw.a;
break;
case R.id.button2:
resId = R.raw.b;
break;
default:
resId = R.raw.a;
break;
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
mp = MediaPlayer.create(this, resId);
mp.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onDestroy() {
if(null!=mp){
mp.release();
}
super.onDestroy();
}
此对象在此处实例化:
import java.awt.*;
import javax.swing.*;
public class drawr extends JPanel{
public static int animationSpeed=470;
public static int diameter = 50;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.decode("#ffc000"));
g.drawLine(0, 0, getWidth(), getHeight());
for(int cnt=0; cnt/2<=displayObject.ob1.noOfVerts; cnt+=2){
g.drawLine(displayObject.ob1.coord[cnt], displayObject.ob1.coord[cnt+1], ob1.coord[cnt+2], ob1.coord[cnt+3]);
}
}
并在此构建:
import java.awt.Color;
import javax.swing.*;
public class MainClass{
public static final int windowWidth = 1280;
public static final int windowHeight = 640;
public static boolean crash = false;
public static displayObject ob1, ob2, ob3;
public static void main(String[] args)
throws InterruptedException {
String colorString="#ff0000";
int ob1verts[]={10,10,50,50,30,80};
displayObject ob1=new displayObject("#ff0000", ob1verts);
}
}
答案 0 :(得分:2)
除了你的代码乱七八糟的事实,这一行
displayObject ob1 = new displayObject("#ff0000", ob1verts);
应该只是:
ob1 = new displayObject("#ff0000", ob1verts);
您已声明ob1,ob2&amp;代码中的ob3,即
public static displayObject ob1, ob2, ob3; // ob1 declared
至于您的错误, ob1 是displayObject
类的静态变量,
并且您无法使用displayObject.ob1
访问它
您需要将 ob1 的引用从MainClass
传递给drawr
。
或者你可能意味着MainClass.ob1
?因为它在MainClass
中声明为静态变量。