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

Compile error: "Lambda expressions are not supported at this language level."

Just wondering how I can convert this code to be supported. I didn't have a lot of luck trying to get lambda expressions from Java 8 to work for me. I have 3 instances of lambda expressions (line 33, line 46 and line 59.

Also this is based on the code for the Fun Facts training module in Tree House.

 package com.joshbgold.spanishwordoftheday;

 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.view.View;
 import android.widget.Button;
 import android.widget.RelativeLayout;
 import android.widget.TextView;

 public class MainActivity extends ActionBarActivity {

private PirateWords mPirateWords = new PirateWords();
private ColorRandomizer mColorRandomizer = new ColorRandomizer();
private int currentNumber = 0;
private boolean stayHere = true; //while true, will use to keep running the program

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

    //Declare our view variables & assign views from the layout file
    final TextView pirateLabel = (TextView) findViewById(R.id.pirateView);
    final Button pirateButton = (Button) findViewById(R.id.pirateButton);
    final Button pirateAnswerButton = (Button) findViewById(R.id.pirateAnswerButton);
    final Button exitButton = (Button) findViewById(R.id.exitButton);
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

 do {
     currentNumber = mPirateWords.getRandomNumber();

     View.OnClickListener listener = (view) -> {

             String fact = mPirateWords.getPirateWord(currentNumber);
             //Update the label with our dynamic fact
             pirateLabel.setText(fact);

             int color = mColorRandomizer.getColor();
             relativeLayout.setBackgroundColor(color);
             pirateButton.setLinkTextColor(color);


     };

     View.OnClickListener listenerAnswer = (view) -> {

             String fact = mPirateWords.getPirateAnswer(currentNumber);
             //Update the label with our dynamic fact
             pirateLabel.setText(fact);

             int color = mColorRandomizer.getColor();
             relativeLayout.setBackgroundColor(color);
             pirateButton.setLinkTextColor(color);


     };

     View.OnClickListener walkThePlank = (view) -> {

         System.exit(0);
     };



     pirateButton.setOnClickListener(listener);
     pirateAnswerButton.setOnClickListener(listenerAnswer);
     exitButton.setOnClickListener(walkThePlank);

 } while (stayHere); //user has not walked the plank
 }
 }

1 Answer

Ok I replaced the lambda expressions (lambda expressions are shown in the original code as arrows). Java 7 SDK does not support lambda expressions it seems. As a beginner in Java, I didn't want to get mired into trying to figure out how to get Java 8 working with Android Studio. I actually spent about 20 minutes working on setting up Java 8, and then I decided to take a break and come back to this later.

The lambda expressions are actually really cool because they require far less code. Maybe they will be more mainstream in 6-12 months, and that style of coding will be second nature for all of us.

Well today I replaced the lambda expressions. I will show you an example of the old code and the new working code:

Old code with lambdas, requires Java 8

     View.OnClickListener walkThePlank = view -> {
                 System.exit(0);
         };

New code without lambdas

   View.OnClickListener walkThePlank = new View.OnClickListener() {
             @Override
             public void onClick (View view) {
                 System.exit(0);
             }
         };