Android 所有的更改都将指向同一个片段,为什么?
我正在创建一个战舰游戏,玩家1在底部屏幕看到他的战舰,而顶部屏幕是他在玩家2的棋盘上移动的地方。但是所有的东西都被发送到player1屏幕。所以所有玩家1的飞船和所有对玩家2的移动都出现在Android 所有的更改都将指向同一个片段,为什么?,android,android-fragments,Android,Android Fragments,我正在创建一个战舰游戏,玩家1在底部屏幕看到他的战舰,而顶部屏幕是他在玩家2的棋盘上移动的地方。但是所有的东西都被发送到player1屏幕。所以所有玩家1的飞船和所有对玩家2的移动都出现在\u p1BattleshipFragment中。这是对片段的限制,还是我在某个地方使用了错误的片段名称(尽管我已经在代码中花了几个小时试图找出原因),还是我在不知不觉中将所有内容发送到该片段?我需要一双新的眼睛和/或解释我到底做错了什么。非常感谢您的帮助 public static final String
\u p1BattleshipFragment
中。这是对片段的限制,还是我在某个地方使用了错误的片段名称(尽管我已经在代码中花了几个小时试图找出原因),还是我在不知不觉中将所有内容发送到该片段?我需要一双新的眼睛和/或解释我到底做错了什么。非常感谢您的帮助
public static final String GAME_INDEX_EXTRA = "game_index";
int _gameIndex = -1;
Game _currentGame;
BattleshipFragment _p2BattleshipFragment;
BattleshipFragment _p1BattleshipFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(rootLayout);
if(getIntent().hasExtra(GAME_INDEX_EXTRA))
{
_gameIndex = getIntent().getExtras().getInt(GAME_INDEX_EXTRA);
if(_gameIndex != -1)
_currentGame = GameLibrary.getInstance().getGame(_gameIndex);
else
_currentGame = new Game();
}
else
{
finish();
}
FrameLayout p2_screen = new FrameLayout(this);
p2_screen.setId(10);
rootLayout.addView(p2_screen, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 0, 49));
View divider = new View(this);
divider.setBackgroundColor(Color.BLACK);
rootLayout.addView(divider, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 0, 2));
FrameLayout p1_screen = new FrameLayout(this);
p1_screen.setId(11);
rootLayout.addView(p1_screen, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 0, 49));
_p2BattleshipFragment = new BattleshipFragment();
_p1BattleshipFragment = new BattleshipFragment();
FragmentTransaction addTransaction = getFragmentManager().beginTransaction();
addTransaction.add(10, _p2BattleshipFragment);
addTransaction.add(11, _p1BattleshipFragment);
addTransaction.commit();
p2_screen.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
_currentGame.gameState = "In Progress";
int id = _p2BattleshipFragment._currentViewId;
int x = 0;
int y = id;
int value = 0;
if(y > 10)
{
x = id/10;
y = id%10;
}
value = _currentGame.markHit(_currentGame.p2, x, y);
switch(value)
{
case -1: // hit
_p2BattleshipFragment.setColor(id, Color.RED);
break;
case 0: // already guessed
break;
case 10: // miss
_p2BattleshipFragment.setColor(id, Color.BLACK);
break;
case 100: // sunk ship
break;
case 1000: // game over
break;
default:
break;
}
return true;
}
});
}
@Override
protected void onStart()
{
super.onStart();
int[][] board = _currentGame.p1._board;
for(int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
int id = 0;
try
{
String idString = Integer.toString(j) + Integer.toString(i);
id = Integer.parseInt(idString);
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
int value = board[i][j];
switch (value)
{
case -1: // hit
_p1BattleshipFragment.setColor(id, Color.RED);
break;
case 0: // already guessed
_p1BattleshipFragment.setColor(id, Color.BLACK);
break;
case 1: // miss
_p1BattleshipFragment.setColor(id, Color.GRAY);
break;
default:
break;
}
}
}
int[][] board2 = _currentGame.p2._board;
for(int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
int id = 0;
try
{
String idString = Integer.toString(j) + Integer.toString(i);
id = Integer.parseInt(idString);
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
int value = board2[i][j];
switch (value)
{
case -1: // hit
_p2BattleshipFragment.setColor(id, Color.RED);
break;
case 0: // already guessed
_p2BattleshipFragment.setColor(id, Color.BLACK);
break;
case 1: // miss
_p2BattleshipFragment.setColor(id, Color.GRAY);
break;
default:
break;
}
}
}
}
publicstaticfinalstringgame\u INDEX\u EXTRA=“GAME\u INDEX”;
int_gameIndex=-1;
游戏(Game);;
战舰碎片p2;战舰碎片;
战舰碎片_p1战舰碎片;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout rootLayout=新的LinearLayout(此);
根布局。设置方向(线性布局。垂直);
setContentView(rootLayout);
if(getIntent().hasExtra(游戏索引额外))
{
_gameIndex=getIntent().getExtras().getInt(GAME\u INDEX\u EXTRA);
如果(_gameIndex!=-1)
_currentGame=GameLibrary.getInstance().getGame(_gameIndex);
其他的
_currentGame=新游戏();
}
其他的
{
完成();
}
FrameLayout p2_屏幕=新的FrameLayout(此);
p2_屏幕设置ID(10);
rootLayout.addView(p2_屏幕,新的LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,0,49);
视图分隔符=新视图(此);
分隔线。背景颜色(颜色。黑色);
rootLayout.addView(分隔符,新的LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,0,2);
FrameLayout p1_屏幕=新的FrameLayout(此);
p1_屏幕设置ID(11);
rootLayout.addView(p1_屏幕,新LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,0,49);
_p2BattleshipFragment=新的BattleshipFragment();
_p1BattleshipFragment=新的BattleshipFragment();
FragmentTransaction addTransaction=getFragmentManager().beginTransaction();
addTransaction.add(10,_p2;片段);
addTransaction.add(11,_p1)是一个片段;
addTransaction.commit();
p2_screen.setOnTouchListener(新视图.OnTouchListener()
{
@凌驾
公共布尔onTouch(视图、运动事件、运动事件)
{
_currentGame.gameState=“进行中”;
int id=_P20;当前视图id;
int x=0;
int y=id;
int值=0;
如果(y>10)
{
x=id/10;
y=id%10;
}
值=_currentGame.markHit(_currentGame.p2,x,y);
开关(值)
{
案例1://点击
_p2.setColor(id,Color.RED);
打破
案例0://已经猜到了
打破
案例10://小姐
_p2.setColor(id,Color.BLACK);
打破
案例100://沉船
打破
案例1000://游戏结束
打破
违约:
打破
}
返回true;
}
});
}
@凌驾
受保护的void onStart()
{
super.onStart();
int[][]板=_currentGame.p1._板;
对于(int i=0;i<10;i++)
{
对于(int j=0;j<10;j++)
{
int id=0;
尝试
{
字符串idString=Integer.toString(j)+Integer.toString(i);
id=Integer.parseInt(idString);
}捕获(例外e)
{
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
int值=板[i][j];
开关(值)
{
案例1://点击
_p1.setColor(id,Color.RED);
打破
案例0://已经猜到了
_p1.setColor(id,Color.BLACK);
打破
案例1://小姐
_p1.setColor(id,Color.GRAY);
打破
违约:
打破
}
}
}
int[][]board2=_currentGame.p2._board;
对于(int i=0;i<10;i++)
{
对于(int j=0;j<10;j++)
{
int id=0;
尝试
{
字符串idString=Integer.toString(j)+Integer.toString(i);
id=Integer.parseInt(idString);
}捕获(例外e)
{
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
整数值=2[i][j];
开关(值)
{
案例1://点击
_p2.setColor(id,Color.RED);
打破
案例0://已经猜到了
_p2.setColor(id,Color.BLACK);
打破
案例1://小姐
_p2.setColor(id,Color.GRAY);
打破
违约:
打破
}
}
}
}
没有xml,因为这是一个赋值,也是指令的一部分——所有事情都是通过编程完成的 您的代码令人困惑(至少使用下划线作为变量前缀不是Java惯例)。还有,为什么要用代码创建布局?使用XML布局文件可以更轻松地完成所需的工作,并且可以删除cod