Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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