Austin So
Austin So

Reputation: 11

New Activity not responding

I have been trying to figure what's wrong on my own for 2 days now, but the second activity just won't start on button click, there are no error messages, no blank pages as if the button doesn't work, am I missing something obvious?

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

Second Activity:

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

Upvotes: 0

Views: 289

Answers (6)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

1. You should call clickSend() method from activity life-cycle method onCreate() to initialize the UI elements( like Button, EditText etc).

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

2. You are calling startActivity() twice. Update clickSend() method as below:

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

Upvotes: 0

akhilesh0707
akhilesh0707

Reputation: 6899

You need to call clickSend() method inside your onCreate() method

Edit: Please check updated answer

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

Upvotes: 2

Iulian Popescu
Iulian Popescu

Reputation: 2643

Already existing answers will solve the problem but I think that you need to understand why your method was not called. Checking the docs for activity, you'll see a bunch of listed methods and onCreate is one of them. Those methods are guaranteed to be called by the system for you (never call one of that methods yourself) and hence there is no need for you to call them. The problem is that the system doesn't know about your clickSend method and will never call it, letting that responsibility in your hands.

Upvotes: 1

taug
taug

Reputation: 378

You must call clickSend() method in onCreate() method.

Then only the onClickListener will work.

And remove calling the startActivity(intent) twice.

Change the code of createMessage class as below

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

Upvotes: 8

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

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

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69671

try this

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


}

Upvotes: 0

Related Questions