Activity intent传值,在get方法中为什么只取到了private int 的原始值

问题:在前一个Activity中设置了传值row和column=4或6,经过测试到mainActivity传值成功。当在gameView中取getRow的值时,每次只取到private中的值6,而不取传过的intent中的值。

代码如下:

public class MainActivity extends Activity {
private int row = 6;
private int column= 6;
public MainActivity() {
mainActivity = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvscore = findViewById(R.id.tvScore);
Intent intent = this.getIntent();
row = intent.getIntExtra("row", 4);
column = intent.getIntExtra("column", 4);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void clearScore() {
score = 0;
showScore();
}

private void showScore() {
// TODO Auto-generated method stub
// if(int s = getMax(CardMap[x][y])){
//
// }
tvscore.setText(score + "");

}

public void addScore(int s) {
score += s;
showScore();
}

private TextView tvscore;
private int score = 0;
public static MainActivity mainActivity = null;

public static MainActivity getMainActivity() {
return mainActivity;
}

public int getColumn() {
return column;
}

public int getRow() {
return row;
}
}

GameView中的部分代码:

private int row, column;
private Cards[][] cardMap = new Cards[10][10];
private ArrayList<Point> emptyPoints = new ArrayList<>();

public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initGame();
}

public void initGame() {
// TODO Auto-generated method stub
row = MainActivity.getMainActivity().getRow();
column = MainActivity.getMainActivity().getColumn();
setColumnCount(column);
setBackgroundColor(getResources().getColor(R.color.grey3));

求大神解答!!!谢谢谢谢!!!
2018-03-01 12:07 添加评论 分享
已邀请:
0

bevis_huang - android

赞同来自:

row=MainActivity.getMainActivity().getRow(); 改为 row=((MainActivity)context).getRow();

要回复问题请先登录注册

退出全屏模式 全屏模式 回复