我按下按钮开始游戏。每当我点击AVD上的那个按钮,不幸的是程序已经停止,我是Android编程的新手。我尝试谷歌搜索我的问题,似乎代码有问题。谁能帮我?我也分享了我的代码链接,请访问https://www.dropbox.com/sh/hyitjbgda69rkd4/3Iz8WuM5-5
主要活动
public class MainActivity extends Activity
{
private Game game1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.startbutton);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
game1 = new Game(this);
setContentView(game1);
}
});
}
}
游戏课程:
public Game(OnClickListener onClickListener)
{
super((Context) onClickListener);
caneta = new Paint();
this.caneta.setARGB(255, 0, 0, 0);
this.caneta.setAntiAlias(true);
this.caneta.setStyle(Style.STROKE);
this.caneta.setStrokeWidth(5);
l = this.getWidth();
a = this.getHeight();
singlesquare = new Cell[x][y];
int xss = l / x;
int yss = a / y;
for (int z = 0; z < y; z++)
{
for (int i = 0; i < x; i++)
{
singlesquare[z][i] = new Empty(xss * i, z * yss);
}
}
}
答案 0 :(得分:0)
你真的不应该多次打电话给setContentView()
。是导致错误的那条线吗?此外,game1
的对象类型似乎不是视图。