简体   繁体   中英

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

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.

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.

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.

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

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