初学者与Android开发有困难

时间:2014-09-15 00:20:29

标签: java android eclipse import

嗨,我现在正在申请家庭作业和墙。我想采取两个输入,如someone名称,然后发送电子邮件,然后显示一个感谢信息,其中包含他们的姓名和电子邮件。我有这个谢谢你的页面,当我点击它时,我无法按下任何按钮。我有这个代码,我没有出现错误。

      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);
    }
    }

和我的其他活动的其他代码

       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);
        }

    }
   }

提前感谢您的任何建议。

2 个答案:

答案 0 :(得分:0)

您在两个活动中都有:

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);

答案 1 :(得分:0)

好的,所以你稍微混淆了名字:P Becouse,SignupActivity指的是activity_thank_you,而ThankYouActivity指的是layout_signup

在你的SingupActivity中改变这个:

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

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

在您的SignupActivity中更改此内容:

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

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

在activity_thank_you中,您创建了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 Manifest中的更改名称。

在onClick方法中,在startActivty之前删除它,如下所示:

this.startActivity(intent); //WAS

startActivity(intent); //SHOULD BE