简体   繁体   中英

Beginner with Android Development having trouble

Hi I am making a application for a homework assignment and at a wall right now. I want to take two inputs such as someones name and then email then display a thank you message with their name and email. I have this for my thank you page which I am having trouble making my button do anything when I click it. I have this for code and I have no errors showing up.

      import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.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);
    }
    }

and my other code for the other activity

       import android.support.v7.app.ActionBarActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.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);
        }

    }
   }

Thank you for any suggestions ahead of time.

You have in both Activities:

setContentView(R.layout.layout_signup);

Change it to your other xml layout.

EDIT: You dont pass email by intent, below passing name:

intent.putExtra("name", name);

put this:

intent.putExtra("emial", email);

It should look like this:

    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: In the 4 line on onClick change 'this' to getApplicationContext(), like this: (if you can)

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

And on the last line delete 'this', it should look like this:

    startActivity(intent);

Ok, so you muddle names a little bit :P Becouse, SignupActivity refers to activity_thank_you and ThankYouActivity reffers to layout_signup

In your SingupActivity change this:

    setContentView(R.layout.layout_signup); //WAS

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

In your SignupActivity change this:

    setContentView(R.layout.layout_signup); //WAS

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

In activity_thank_you you created TextView but you didn't set id for it so change it to this:

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

And when you add it to textview, go to SignupActivity and join it together:

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

Also, don't forget change names in Android Manifest.

And in the onClick method delete this before startActivty, like this:

this.startActivity(intent); //WAS

startActivity(intent); //SHOULD BE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM