有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android开发的java初学者遇到问题

嗨,我正在申请一份家庭作业和一堵墙。我想输入两个信息,比如某人的名字和电子邮件,然后显示一条带有姓名和电子邮件的感谢信息。这是我的感谢页面,当我点击它时,我很难让我的按钮做任何事情。我有这个代码,我没有出现错误

      import 安卓.support.v7.app.ActionBarActivity;
  import 安卓.os.Bundle;
  import 安卓.view.Menu;
  import 安卓.view.MenuItem;
  import 安卓.widget.TextView;

  public class SignupActivity extends ActionBarActivity {

    TextView greetMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_signup);

        Bundle bundle = this.getIntent().getExtras();
        if(null != bundle){
            String name = bundle.getString("name");
            String email = bundle.getString("email");
            if(null != name && name != "" && email != "") {
                greetMessage = (TextView)findViewById(R.id.textView1);
                greetMessage.setText("Thank you " + name + "you can expect and email from us soon to       
  your email address at " + email);
            }
        }



    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }

还有我的其他活动代码

       import 安卓.support.v7.app.ActionBarActivity;
    import 安卓.content.Intent;
    import 安卓.os.Bundle;
    import 安卓.view.Menu;
    import 安卓.view.MenuItem;
    import 安卓.view.View;
    import 安卓.view.View.OnClickListener;
    import 安卓.widget.Button;
    import 安卓.widget.EditText;

    public class ThankYouActivity extends ActionBarActivity implements OnClickListener {

    EditText nameField;
    EditText emailField;
    Button submitButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_signup);

        emailField = (EditText) findViewById(R.id.editText2);
        nameField = (EditText) findViewById(R.id.editText1);
        submitButton = (Button) findViewById(R.id.button1);

        submitButton.setOnClickListener(this);


    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        String name = nameField.getText().toString();
        String email = emailField.getText().toString();
        if (null != name && name != "" && email != "") {
            Intent intent = new Intent(this,ThankYouActivity.class);
            intent.putExtra("name", name);
            this.startActivity(intent);
        }

    }
   }

感谢您提前提出建议


共 (2) 个答案

  1. # 1 楼答案

    你在这两项活动中都有:

    setContentView(R.layout.layout_signup);
    

    将其更改为其他xml布局

    编辑: 你不会故意在传递姓名下面传递电子邮件:

    intent.putExtra("name", name);
    

    请说:

    intent.putExtra("emial", email);
    

    应该是这样的:

        public void onClick(View v){
    
        String name = nameField.getText().toString();
        String email = emailField.getText().toString();
        if (null != name && name != "" && email != "") {
            Intent intent = new Intent(this,ThankYouActivity.class);
            intent.putExtra("name", name);
            intent.putExtra("emial", email);
            this.startActivity(intent);
        }
    }
    

    EDIT2: 在onClick的4行中,将“this”更改为getApplicationContext(),如下所示:(如果可以的话)

        Intent intent = new Intent(getApplicationContext(),ThankYouActivity.class);
    

    在最后一行删除“this”,它应该是这样的:

        startActivity(intent);
    
  2. # 2 楼答案

    好吧,你把名字搞混了一点:p 因为,注册活动指的是“谢谢”活动,而“谢谢”活动指的是“布局注册”

    在SingupActivity中,更改以下内容:

        setContentView(R.layout.layout_signup); //WAS
    
        setContentView(R.layout.activity_thank_you); //SHOULD BE
    

    在您的注册活动中,更改以下内容:

        setContentView(R.layout.layout_signup); //WAS
    
        setContentView(R.layout.activity_thank_you); //SHOULD BE
    

    在活动_谢谢_您创建了TextView,但您没有为其设置id,所以将其更改为:

       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:id="@+id/randomName"/>
    

    当您将其添加到textview时,请转到SignupActivity并将其连接在一起:

        greetMessage = (TextView)findViewById(R.id.randomName);
    

    另外,不要忘记在Android清单中更改名称

    在onClick方法中,在startActivty之前删除以下内容:

    this.startActivity(intent); //WAS
    
    startActivity(intent); //SHOULD BE