简体   繁体   中英

Trouble with Intent in Android Studio

I have been trying to get a string to go from one java class to another for a project of mine. The code I have been experimenting with is not working. When I press the button, I know it opens the other Java class because it creates the other layout, but it doesn't show the string. Please help me.

First Java Class:

public class MainActivity extends AppCompatActivity {

private Button button;
Context context;
private EditText editText;
String number = null;

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

    context = this;

    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getText().toString() != null) {
                String value = "value";
                Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
                secondscreenIntent.putExtra("Number", editText.getText().toString());
                startActivity(secondscreenIntent);


            }
        }
    });
}
}

Second Java Class:

public class SecondScreenJ extends Activity {

String number = null;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen);

    textView = (TextView) findViewById(R.id.textView);

    Bundle extras = getIntent().getExtras();
    if (extras != null){
        number = extras.getString("number");
    }

    textView.setText(number);

}


}

you are putting "Number" as key but, in your second activity you are trying to retrieve "number" so change number to Number and it will work. Its case Sensitive.

Don't make your keys hard-coded in that way. Just declare public static variable in MainActivity and use it from SecondScreenJ

public class MainActivity extends AppCompatActivity {

private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;

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

context = this;

editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (editText.getText().toString() != null) {
            String value = "value";
            Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
            secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
            startActivity(secondscreenIntent);


        }
    }
});
 }
 }

Second Java Class:

public class SecondScreenJ extends Activity {

String number = null;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.secondscreen);

 textView = (TextView) findViewById(R.id.textView);

 Bundle extras = getIntent().getExtras();
 if (extras != null){
    number = extras.getString(MainActivity.NUMBER_KEY);
 }

 textView.setText(number);

 }


}

Beware when using the key for putting and getting the extras. They're case sensitive. Replace "number" with "Number" in your second activity.

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