user2465510
user2465510

Reputation: 99

Confused what is wrong with Intent constructor

I'm trying to create a button that opens a second activity. I've looked over the tutorials that I could find on Intents, and I've though I was following them correctly, but clearly I'm not.

NewScreenActivity is a completely blank activity. What is wrong with the Intent constructor and how can I do it correctly?

//NewScreenActivity in same package
package com.example.intri.firstexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView userText;
    Button buttonToNewScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userText = (TextView) findViewById(R.id.userText);
        buttonToNewScreen = (Button) findViewById(R.id.buttonToNewScreen);
        userText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = userText.getText().toString();
                Intent toNewScreen = new Intent(this, NewScreenActivity.class);

            }
        });
    }
}

Thanks

Upvotes: 0

Views: 84

Answers (4)

Vidya Sagar
Vidya Sagar

Reputation: 180

Use the following inside you OnCLickListener for Intent. "this" inside your Intent refers to the clickListener. So you have to specify the activity name and later you have to start the activity

Intent toNewScreen = new Intent(MainActivity.this, NewScreenActivity.class);
startActivity(toNewScreen);

Let me know if this works.

Upvotes: 0

kostyabakay
kostyabakay

Reputation: 1689

Of course it is blank activity, because you didn't send any data via Intent. Your Intent just opens another Activity. You should use Extras.

String input = userText.getText().toString();
Intent toNewScreen = new Intent(MainActivity.this, NewScreenActivity.class);
toNewScreen.putExtra("data", input);
startActivity(toNewScreen);

Upvotes: -1

marcinj
marcinj

Reputation: 49976

Your code should not compile, the correct code should look as follows:

Intent intent = new Intent(MainActivity.this, NewScreenActivity.class);
startActivity(intent);

your blank activity issue might be entirely different problem. If you want to pass data with intent then use Intent.putExtra family of functions.

Upvotes: 1

slanecek
slanecek

Reputation: 808

userText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = userText.getText().toString();
                Intent toNewScreen = new Intent(this, NewScreenActivity.class);

            }
        });

You cannot use "this" in new Intent() in this case, because "this" refers to the new OnClickerListener anonymous class. Instead of "this" you should call something like getApplicationContext(). If you want to refer to your activity, write a method named openNewActivity() (or something like that) and start it from there.

Upvotes: 2

Related Questions