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 trialJames N
17,864 Pointsi have errors!!!!! HELP!!!
my code for main list activity is:
package james.blogreader;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainListActivity extends ListActivity {
protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
protected NetworkInfo networkInfo;
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()) {
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
}
else {
Toast.makeText(this,"No network detected.",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 = manager.getActiveNetworkInfo();
return false;
}
@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);
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
public void updateList() {
if (mBlogData == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.title));
builder.setMessage(getString(R.string.error_message));
builder.setPositiveButton(android.R.string.ok,null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
try{
JSONArray jsonPosts = mBlogData.getJSONArray("posts");
mBlogPostTitles = new String[jsonPosts.length()];
for (int i = 0; i < jsonPosts.length();i++) {
JSONObject post = jsonPosts.getJSONObject(i);
String title = post.getString("title");
title = Html.fromHtml(title).toString();
mBlogPostTitles[i] = title;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mBlogPostTitles);
setListAdapter(adapter);
}
catch (JSONException e) {
Log.e() (TAG, "Exception caught", e)
}
}
}
private class GetBlogPostsTask extends AsyncTask<Object,Void,JSONObject> {
@Override
protected JSONObject doInBackground(Object... arg0) {
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 Responce 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;
protected void updateList();
}
}
@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();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
}
my errors are:
) expected
; expected
cannot resolve method e()
unexpected token
; expected
expression expected
i also have warnings, which are:
Method updateList() is never used
please help!!! i will thank you
2 Answers
Henry Goh
20,201 PointsHi James,
There is a syntax error in the catch
exception under the updateList()
method.
It looks like an error to me anyway.
The warning message is preety much self-explanatory, the updateList()
method is not being used by any other classes in your Android project. If you can be sure it is not used in the future, then you may remove it to reduce your file size and save compiling time.
Best regards,
Henry
Henry Goh
20,201 PointsOh and by the way, your isNetworkAvailable()
method will always return false
, so you will keep getting "No network detected." message.
Maybe you can try changing the return
statement to
return (networkInfo != null);
This will return true
if the networkInfo
variable is not null
and return false
if it is null
.
Best regards,
Henry
James N
17,864 PointsJames N
17,864 Pointsit says protected void updateList(); on the onPostExecute thingy mabobber. how do i fix my error in my catch block?
Henry Goh
20,201 PointsHenry Goh
20,201 PointsFrom what I can see here, provided that I copy your code directly, these are the things that you need to fix.
Line 96
Change
Log.e() (TAG, "Exception caught", e)
toLog.e(TAG, "Exception caught", e);
Line 143
You are declaring the
updateList()
method within another method which is wrong.I think you're suppose to call it so just enter
updateList();
will do just fine.Let me know how it goes.
Best regards,
Henry
James N
17,864 PointsJames N
17,864 Pointsthanks, it works! but i get a warning that says isNetworkAvailible recurses infinitly and can only end by thriwing an exeption.
thanks for your help!!!
Henry Goh
20,201 PointsHenry Goh
20,201 PointsDid Android Studio specify what kind of exception?
I don't like the idea of throwing an Exception.
You can put a
try-catch
statement in the code block instead.James N
17,864 PointsJames N
17,864 Pointsit does not specify what kind of exeption it will throw.
Henry Goh
20,201 PointsHenry Goh
20,201 PointsThen you can use the
Exception
class instead.