Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App Intents and Multiple Activities Getting Data from an Intent

failing to declare an intent variable and getting it to start an activity

This is the error that I keep getting

./FlightActivity.java:14: error: cannot find symbol String name = intent.getStringExtra("fuelLevel"); ^ symbol: method getStringExtra(String) location: variable intent of type Intent ./FlightActivity.java:15: error: cannot find symbol Log.d(TAG,name); ^ symbol: variable TAG location: class FlightActivity ./FlightActivity.java:15: error: cannot find symbol Log.d(TAG,name); ^ symbol: variable Log location: class FlightActivity 3 errors

FlightActivity.java
import android.os.Bundle;

public class FlightActivity extends AppCompatActivity {

    public int fuelLevel = 0;

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

        // Add your code below!
        Intent intent = getIntent();
        String name = intent.getStringExtra("fuelLevel");
        Log.d(TAG,name);
    }
}

2 Answers

Nikos Tzouvelekis
Nikos Tzouvelekis
8,155 Points

Hi :) be Careful , here you don't have a String but a int, also read carefully what exercise ask :)

so the : String name = intent.getStringExtra("fuelLevel"); its false the right is : fuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);

also you don't must write : Log.d(TAG,name); because the exercise don't ask about this

import android.os.Bundle;

public class FlightActivity extends AppCompatActivity {

    public int fuelLevel = 0;

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

        // Add your code below!
        Intent intent = getIntent();
        fuelLevel = intent.getIntExtra("FUEL_LEVEL", -1); 
    }
}

:)

Thank you..... How did u get that default value ?

Nikos Tzouvelekis
Nikos Tzouvelekis
8,155 Points

I didn't get it , I choose this default value ,, so if the value from "extras" is null then set the fuelLevel with this default value .

Actually when a string has no value then return null ,, when a int has no value then return -1 .. play with that open the android studio with a new project and write for example ….

int a; string b; (with no values) then write …

Log.d(TAG,a); Log.d(TAG,b);

and check at the logcat what the empty integers , strings , etc. that returns :)