EditText 并使用按钮提交它们
所以基本上我希望允许用户输入文本(他们的名字)。然后单击提交按钮,该按钮将该名称存储到数组中并删除 EditText 中的文字(并为玩家创建一个计数器)。在他们提交完玩家姓名后,我希望他们能够单击播放按钮(我相信标题为“完成”)并继续到下一页,所有信息都被发送过来。
我目前的问题是,当我输入名称并单击“提交”时,它会强制关闭。如果我单击播放按钮,它会强制关闭。你觉得你能帮我吗?感谢
1班:
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);
Button submitButton = (Button) findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View submit1)
{
players++;
for(int i=0; i < players; i++)
{
names[i] = 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, Class2.class);
done.putExtra("players", players);
done.putExtra("names", names[players]);
startActivity(done);
}
});
}
So basically I would like this to allow the user to input text (their name). Then to click the submit button which will store that name into an array and erase the writing in the EditText (as well as make a counter for players). After they are done submitting players names I want them to be able to click the play button (titled done i believe) and continue to the next page with all the information being sent over.
My problem currently is when i enter in a name and click submit it force closes. If i click the play button it force closes. Think you could help me out? Thanks
Class 1:
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);
Button submitButton = (Button) findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View submit1)
{
players++;
for(int i=0; i < players; i++)
{
names[i] = 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, Class2.class);
done.putExtra("players", players);
done.putExtra("names", names[players]);
startActivity(done);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有在任何地方分配变量
名称
。你应该这样做:如果你不确定
NO_OF_PLAYERS
,那么使用并使用它:
You're not allocating for variable
names
anywhere. You should do this:If you're not sure about
NO_OF_PLAYERS
, then useand use it:
您的程序崩溃是因为您尝试访问未初始化的数组。您必须使用
String names[] = new String[MAX_COUNT]
创建数组对象。Your program crashes because you're trying to access non-initialized array. You must create array object using
String names[] = new String[MAX_COUNT]
.我不确定这可能是原因,但需要注意:
您的提交按钮的 onclick 方法:
在这里您增加
players
计数器。然后从0循环到玩家计数器。在循环中,您在名称数组中分配输入文本。最后清除输入。这不是正确的做法。您尚未初始化名称数组。另外你不应该每次都循环。它将覆盖名称数组中的旧值。下次在循环中 input.getText() 将返回空白,因为您在第一次进入循环时已经从输入中删除了文本。
如果您调试代码,您应该
会更好地理解我想说的内容。
I'm not sure this could be the reason but needs attention:
your onclick method of submit button:
Here you increase
players
counter. Then loop from 0 to players counter. In loop you assign the input text in names array. Finally you clear the input.This is not the right way to do. You have not initialized names array. Also you should not loop every time. It will override the old values in names array. And next time in loop input.getText() will return blank cause you have erased text from input already in first time you entered the loop.
It should be
if you debug your code you will better understand what I'm trying to say.
请尝试这个编辑过的代码,
Please try this edited code,