我正在将一个Windows对游戏(用java编写)导入到android 但是当我跑这个时我看不到任何按钮...... (目前我的代码看起来不太好,因为我应该快速将它发送到uni)
这里出了什么问题?
package mgh;
import mgh.mgh.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.RelativeLayout;
public class mghActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new mghActivity();
}
public int numberOfClickes=0;
public card continueCard=null;
public int killedCards=0;
public card selectedCard=null;
public long spentTime=0;
public card[][] card=new card[4][4];
public void mghActivity(){
setTitle("pairs");
//setVisible(true);
//setSize(500,500 );
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AbsoluteLayout lay=new AbsoluteLayout(this);
int[] clr=new int[8];
short[] timesUsed=new short[8];
for (int i=0;i<=7;i++)
timesUsed[i]=0;
for (int i=0;i<=7;i++)
clr[i]= Color.rgb(((int)(Math.random()*2))*255,
((int)(Math.random()*2))*255,
((int)(Math.random()*2))*255);
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++){
int rnd=0;
while (timesUsed[rnd]>=2)
rnd=(int)(Math.random()*8)%8;
timesUsed[rnd]++;
card[i][j]=new card(this,clr[rnd]);
//card[i][j].setEnabled(false);
//AbsoluteLayout.LayoutParams mparams=new AbsoluteLayout.LayoutParams
//(lay.getWidth()/4,lay.getHeight()/4,(lay.getWidth()/4)*i,(lay.getWidth()/4)*j);
int m=40;
AbsoluteLayout.LayoutParams mparams=new AbsoluteLayout.LayoutParams
(m,m,m*i,m*j);
lay.addView(card[i][j],mparams);
card[i][j].setOnClickListener(this);
}
setContentView(lay);
}
public void onClick(View arg0) {
}
}
class card extends Button {///not completed
public int color;
public card(Context context,int color){
super(context);
this.color=color;
setBackgroundColor(color);
}
}
答案 0 :(得分:0)
在onCreate方法中,您只是新mghActivity
但未调用方法mghActivity
。 mghActivity
不是构造函数。您应在void
之前移除mghActivity
。然后它将是一个构造函数并自动调用。即便如此,我也不建议像这样创建一个Activity。