EditText 并使用按钮提交它们

发布于 2024-11-03 06:36:40 字数 1305 浏览 0 评论 0原文

所以基本上我希望允许用户输入文本(他们的名字)。然后单击提交按钮,该按钮将该名称存储到数组中并删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十雾 2024-11-10 06:36:40

您没有在任何地方分配变量名称。你应该这样做:

names = new String[NO_OF_PLAYERS];

如果你不确定 NO_OF_PLAYERS,那么使用

ArrayList<String> names = new ArrayList<String>();

并使用它:

names.add(input.getText().toString());

You're not allocating for variable names anywhere. You should do this:

names = new String[NO_OF_PLAYERS];

If you're not sure about NO_OF_PLAYERS, then use

ArrayList<String> names = new ArrayList<String>();

and use it:

names.add(input.getText().toString());
野の 2024-11-10 06:36:40

您的程序崩溃是因为您尝试访问未初始化的数组。您必须使用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].

无戏配角 2024-11-10 06:36:40

我不确定这可能是原因,但需要注意:

您的提交按钮的 onclick 方法:

  public void onClick(View submit1)
    {
        players++;
        for(int i=0; i < players; i++)
        {
            names[i] = input.getText().toString();
            input.setText("");
        }
    }

在这里您增加 players 计数器。然后从0循环到玩家计数器。在循环中,您在名称数组中分配输入文本。最后清除输入。

这不是正确的做法。您尚未初始化名称数组。另外你不应该每次都循环。它将覆盖名称数组中的旧值。下次在循环中 input.getText() 将返回空白,因为您在第一次进入循环时已经从输入中删除了文本。

如果您调试代码,您应该

public void onClick(View submit1)
{
    names[players++] = input.getText().toString();
    input.setText("");
}

会更好地理解我想说的内容。

I'm not sure this could be the reason but needs attention:

your onclick method of submit button:

  public void onClick(View submit1)
    {
        players++;
        for(int i=0; i < players; i++)
        {
            names[i] = input.getText().toString();
            input.setText("");
        }
    }

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

public void onClick(View submit1)
{
    names[players++] = input.getText().toString();
    input.setText("");
}

if you debug your code you will better understand what I'm trying to say.

哎呦我呸! 2024-11-10 06:36:40

请尝试这个编辑过的代码,

  public class Class1 extends Activity
    {  
    static int players;
    static String names[];
    static  //this is called once when class is first time used.
   {
     players=-1;
   }

    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++;
                names[players] = 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);
            }
        });
    }

Please try this edited code,

  public class Class1 extends Activity
    {  
    static int players;
    static String names[];
    static  //this is called once when class is first time used.
   {
     players=-1;
   }

    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++;
                names[players] = 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);
            }
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文