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 trialtshuutheniemvula
7,251 Points@Override throws "method does not override method from its superclass" error.
Here is my code:
public class MainListActivity extends ListActivity {
protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
protected JSONObject mBlogData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
if (isNetworkAvailable()) {
GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
getBlogPostTask.execute();
}
else {
Toast.makeText(this, "Network is Unavailable", Toast.LENGTH_LONG).show();
}
//Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void updateList() {
if (mBlogData == null){
//TODO: Handle error
}
else{
try {
Log.d(TAG,mBlogData.toString(2));
} catch (JSONException e) {
Log.e(TAG, "Exception error caught!",e);
}
}
}
private class GetBlogPostTask extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object[] objects) {
int responseCode = -1;
JSONObject jsonResponse = null;
try {
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char[] charArray = new char[contentLength];
reader.read(charArray);
String responseData = new String(charArray);
jsonResponse = new JSONObject(responseData);
}
else {
Log.i(TAG, "Unsuccessful Http Response Code:" + responseCode);
}
}
catch(MalformedURLException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (IOException e){
Log.e(TAG, "Exception caught: ", e);
}
catch (Exception e){
Log.e(TAG, "Exception caught: ", e);
}
return jsonResponse;
}
@Override
protected void OnPostExecute(JSONObject result){
mBlogData = result;
updateList();
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If I remove @Override statement it compiles fine but the mBlogData object does not return any JSON data in logcat.
3 Answers
Steve Hunter
57,712 PointsShould OnPostExecute
be onPostExecute
? Not sure it should be capitalised at the beginning.
Steve.
Steve Hunter
57,712 PointsThat's not clear, to me. When is this error being discovered; during compile? Which @Override
is the offending one?
It sounds like a scope issue but massive blocks of code like that are hard to check on here. It is impossible to scroll to the end of each line.
tshuutheniemvula
7,251 PointsHi Steve, sorry about my vagueness. The error occurs in editor before compile specifically at the onPostExecture method:
@Override protected void OnPostExecute(JSONObject result){ mBlogData = result; updateList();
rll
4,166 Points"method does not override method from its superclass"
In my experience this means that there is an error somewhere in the way you typed the name of YOUR overriding method. The reason it can't find the superclass is because there is no superclass by that name. Check it and recheck it again to make sure the syntax is exactly right- camel casing and all. One caveat with this is that for some reason Android Studio sometimes doesn't autocomplete like Eclipse does. You then need to find the method online or in the class docs and type it from there.
tshuutheniemvula
7,251 Pointstshuutheniemvula
7,251 PointsActually I had onPostexecute, and that was causing the error.
James N
17,864 PointsJames N
17,864 PointsThanks! this helped me as well with a completely non-treehouse related project!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHey - always happy to help, James N!
Steve.