Android 按钮将我转到下一页并开始游戏
在标题中很难解释。基本上就我所知,submit按钮将获取名称并按我所希望的方式将其放置在数组中。我现在需要的是Playdone按钮将用户和数据传输到下一个类,我认为这是正确编码的,我需要它来启动游戏。你将在第二个类中看到的游戏从上一个类中获取数据,我需要它一次显示名称数组1中的名称,并从任务数组中随机为它们分配任务 当前,我单击“播放”按钮后,应用程序将强制关闭。我将这两个类链接起来,因为我很确定问题出在第二个类中。提前谢谢你的帮助 第一类Android 按钮将我转到下一页并开始游戏,android,button,Android,Button,在标题中很难解释。基本上就我所知,submit按钮将获取名称并按我所希望的方式将其放置在数组中。我现在需要的是Playdone按钮将用户和数据传输到下一个类,我认为这是正确编码的,我需要它来启动游戏。你将在第二个类中看到的游戏从上一个类中获取数据,我需要它一次显示名称数组1中的名称,并从任务数组中随机为它们分配任务 当前,我单击“播放”按钮后,应用程序将强制关闭。我将这两个类链接起来,因为我很确定问题出在第二个类中。提前谢谢你的帮助 第一类 public class Class1 extends
public class Class1 extends Activity
{
int players=0;
String names[];
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.class1);
final EditText input = (EditText) findViewById(R.id.nameinput);
final ArrayList<String> names = new ArrayList<String>();
Button submitButton = (Button) findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View submit1)
{
players++;
for(int i=0; i < 6; i++)
{
names.add(input.getText().toString());
input.setText("");
}
}
});
Button doneButton = (Button) findViewById(R.id.done_btn);
doneButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View done1)
{
Intent done = new Intent(Class1.this, Game.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("arrayKey", names);
done.putExtra("players", players);
//done.putExtra("names", names[players]);
startActivity(done);
}
});
}
游戏课
public class Game extends Activity
{
int players, counter=0, score, ptasks,rindex;
String[] names, tasks;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Bundle bundle = this.getIntent().getExtras();
String[] names = bundle.getStringArray("arrayKey");
Intent game = getIntent();
players = game.getIntExtra("players", 1);
//names = game.getStringArrayExtra("names");
Random generator = new Random();
tasks[0]= "";
tasks[1]= "";
tasks[2]= "";
tasks[3]= "";
tasks[4]= "";
tasks[5]= "";
tasks[6]= "";
tasks[7]= "";
tasks[8]= "";
tasks[9]= "";
while (counter <5)
{
for (int i = 0; i < players; i++)
{
TextView name1 = (TextView) findViewById(R.id.pname);
name1.setText( names[i]+":");
ptasks = 10;
rindex = generator.nextInt(ptasks);
TextView task = (TextView) findViewById(R.id.task);
task.setText( tasks[rindex]);
}
Button failButton = (Button) findViewById(R.id.fail_btn);
failButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View failed)
{
}
});
Button notButton = (Button) findViewById(R.id.notbad_btn);
notButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View notbad)
{
}
});
Button champButton = (Button) findViewById(R.id.champ_btn);
champButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View champp)
{
}
});
counter++;
}
}
我想我还应该提到,在第二页上的这些按钮,我想给任何一个名字数组的人分配一个分数,并跟踪直到最后一轮,在那里它将显示赢家。如果有人知道如何使其工作。您是否确保将游戏活动作为应用程序节点包含在AndroidManifest中 如果没有,在底部点击Add,打开你的清单到application选项卡,并添加一个名为.Game的活动
如果没有这一点,另一个类将永远不会收到该意图。您已经被告知在此处使用未初始化的数组:。但你们也试图得到额外的数组,但你们并没有把它放在意图里面。您正在游戏类中使用未初始化的任务数组。每次强制关闭都应在DDMS控制台中打印一个堆栈跟踪。你能和我们分享一下吗?是的,就在那里。我看不出代码有任何问题,我可以说,这就是为什么它让我困惑的原因。我不知道还有什么其他方法可以帮助我们,除非你能向我们展示你的日志或stacktrace,以找到你的强制关闭问题背后的根源。在调试下运行它,然后把坏东西复制给我们。你是对的。我屏蔽了某些部分,它让我进入下一页。直到我解锁了任务,它才出错。我注释掉了getarrayextra,因为我保留了它,除非以前的帮助不起作用,否则我可以将它带回来尝试其他人。你认为初始化任务数组的最佳方法是什么?所以我初始化了任务和名称,现在它打开屏幕而不强制关闭,并且随机选择我想要的任务。现在的问题是1。名称转换不正确,请参见2。仍然不确定如何为名称设置分数?0。例如,字符串[]任务=新字符串[最大任务计数]。1.名称不会转移,因为您不会转移它们。Call done.putExtranames,要传输名称的名称。3.使用地图。