0

I am following the Sams 24hr programming book, but for the life of me cannot seem to fix an issue i am having with Hour 2.

I am trying to create a simple button that launches a second activity and changes the TextArea on the second activity.

This is my code for the second activity. I am getting "Expression Expected" on the Intent in "Intent= getIntent();" and "getStringExtra" is non static method cannot be referenced from static content.

My code looks the same as my book :S

package co.jamesbrown.hour2app;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Second extends AppCompatActivity {

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

        Intent= getIntent();
        String message = Intent.getStringExtra("co.jamesbrown.MESSAGE");
        TextView messageTextView = (TextView) findViewById(R.id.message);
        messageTextView.setText(message);
    }
}

Thanks in advance

James

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Use some IDE, Like Android studio and all. you will get to know the compile time errors at the time of writing the code. Check [here](https://developer.android.com/studio/index.html) – Ashish M Mar 13 '17 at 04:37

3 Answers3

2

Correct it to as below

 Intent intent= getIntent();
 String message = intent.getStringExtra("co.jamesbrown.MESSAGE");
Jinal
  • 44
  • 3
1

Intent= getIntent(); this is not a Java expresssion. You need to give a name while declaring the variable in Java like this :

Intent yourIntent = getIntent();

then you can do :

String message = yourIntent.getStringExtra("co.jamesbrown.MESSAGE");

to get the String value passed through the Intent.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
0

Apart from the answer given , you can also write it in another way

String message = getIntent().getStringExtra("co.jamesbrown.MESSAGE");

Source : How do I get extra data from intent on Android?

Community
  • 1
  • 1
John Joe
  • 12,412
  • 16
  • 70
  • 135