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

SDK update lead to errors

I successfully completed running my app until i updated my SDK. I started receiving errors in my source code that weren't there before, such as:

'cannot return a value from a method with void result type'; symbols that are expected ')' or ';' unexpected token; 'cannot resolve symbol item' - which i think i didn't create, rather it came automatically upon the creation of the project; 'variable listener is accessed from within inner class, needs to be declared final' 'annotations are not allowed here' - red error line beneath the override, which did came automatically.

I tried looking online and didn't find the answer. one advice was to delete the app/build and make gradle rebuild it. but the rebuild failed due to: 'error identifier expected'; 'symbol ; is expected'; 'error reached end of file while parsing'; 'execution failed for task app:compileDebugjava'. This problem is delaying my progress in treehouse.

Thanks in advance.

2 Answers

Everything you just posted leans towards you being short a } somewhere. Also, check your methods and see if you are actually trying to return a value in a method marked with void, you cannot do that.

If you post your code here, i'll fix it for you.

package com.example.FunFactsi.funfacts;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.Random;

public class FunFactsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener = new View.OnClickListener() {
       @Override
       public void onClick(View view) {

           String fact = "";

           Random randomGenerator  = new Random(); // constructing a new random generator
          int randomNumber = randomGenerator.nextInt(3);
          if (randomNumber == 3) {
              factLabel.setText("works");
           }else{
              factLabel.setText("doesn't");
          }

   };
    showFactButton.setOnClickListener(listener);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.fun_facts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Between these two lines

        showFactButton.setOnClickListener(listener);

}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

Add another closing bracket, right after the first one. So it should look like this

        showFactButton.setOnClickListener(listener);

}

}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

Essentially, add another '}' between showFactButton.setOnClickListener
and onCreateOptionsMenu.