新活动没有响应

时间:2017-07-25 06:22:11

标签: java android android-layout android-activity

我一直在努力弄清楚自己2天内出了什么问题,但是第二项活动刚刚开始点击按钮,没有错误信息,没有空白页,好像是按钮不起作用,我错过了一些明显的东西吗?

public class createMessage extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "message";

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

    Button b ;

    public void clickSend(){
        b = (Button) findViewById(R.id.send);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(createMessage.this,receiveMessage.class);
                startActivity(intent);
                EditText editText = (EditText) findViewById(R.id.typeMessage);
                String message = editText.getText().toString();
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
    }
}

第二项活动:

public class receiveMessage extends AppCompatActivity {

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


        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(createMessage.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = (TextView) findViewById(R.id.showMessage);
        textView.setText(message);
    }
}

6 个答案:

答案 0 :(得分:8)

您必须在clickSend()方法中调用onCreate()方法。

然后只有onClickListener可以使用。

然后删除两次调用startActivity(intent)

更改createMessage类的代码,如下所示

public class createMessage extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "message";

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

    Button b ;

    public void clickSend(){
        b = (Button) findViewById(R.id.send);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(createMessage.this,receiveMessage.class);
                EditText editText = (EditText) findViewById(R.id.typeMessage);
                String message = editText.getText().toString();
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
     }
    }

答案 1 :(得分:2)

您需要在clickSend()方法

中调用onCreate()方法

修改:请检查更新后的答案

public class createMessage extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "message";
    Button b;
    EditText editText;

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

    public void clickSend(){
        b = (Button) findViewById(R.id.send);
        editText = (EditText) findViewById(R.id.typeMessage);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message = editText.getText().toString();
                Intent intent = new Intent(createMessage.this,receiveMessage.class);
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
    }
}

答案 2 :(得分:1)

现有的答案将解决问题,但我认为您需要了解为什么不调用您的方法。检查activity的文档,您会看到一堆列出的方法,onCreate就是其中之一。系统会为您保证这些方法(永远不会自己调用其中一种方法),因此您无需调用它们。问题是系统不知道你的clickSend方法,也永远不会打电话给你,让你负责。

答案 3 :(得分:0)

试试这个

public class createMessage extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "message";
Button b ;

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

    b = (Button) findViewById(R.id.send);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(createMessage.this,receiveMessage.class);
            EditText editText = (EditText) findViewById(R.id.typeMessage);
            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }
    });
}  


}

答案 4 :(得分:0)

public class createMessage extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "message";
    Button b ;
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_message);
        editText = (EditText) findViewById(R.id.typeMessage);
        b = (Button) findViewById(R.id.send);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(createMessage.this,receiveMessage.class);
                String message = editText.getText().toString();
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
    }
   }

答案 5 :(得分:0)

1。您应该从活动生命周期方法clickSend()调用onCreate()方法来初始化UI元素(如Button,EditText等)。

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

2。您正在拨打startActivity()两次。更新clickSend()方法如下:

Button b ;
EditText editText;

public void clickSend(){
    b = (Button) findViewById(R.id.send);
    editText = (EditText) findViewById(R.id.typeMessage);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(createMessage.this,receiveMessage.class);

            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }
    });
}