i'm getting this error while running this code, i'm following treehouse Build a blog reader android app and now i'm getting this error
Error:(120, 52) error: no suitable constructor found for ArrayAdapter(MainListActivity.GetBlogPostsTask,int,String[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable (argument mismatch; MainListActivity.GetBlogPostsTask cannot be converted to Context) constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable (argument mismatch; MainListActivity.GetBlogPostsTask cannot be converted to Context) constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable (argument mismatch; MainListActivity.GetBlogPostsTask cannot be converted to Context)
now i'm getting error in this piece of code
private void udpateList() {
if(blogData == null){
// TODO: handle error
}else{
try {
JSONArray jsonPosts = blogData.getJSONArray("posts");
blogPostTitles = 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();
blogPostTitles[i] = title;
}
// !!!!!!!!!! getting error here !!!!!!!!!!!!!
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, blogPostTitles);
setListAdapter(adapter);
} catch (JSONException e) {
Log.e(TAG, "Exception caught:", e);
}
}
}
for easy understanding i've copied whole code, just in case i'm missing out something, i'm following the tutorials and the author is not getting any error while i'm getting error, what might be the problem
package com.example.android.treehouseblogs;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
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[] blogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
protected JSONObject blogData;
@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, R.string.network_not_availabel,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;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_list, menu);
return true;
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... params) {
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) {
blogData = result;
udpateList();
}
private void udpateList() {
if(blogData == null){
// TODO: handle error
}else{
try {
JSONArray jsonPosts = blogData.getJSONArray("posts");
blogPostTitles = 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();
blogPostTitles[i] = title;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, blogPostTitles);
setListAdapter(adapter);
} catch (JSONException e) {
Log.e(TAG, "Exception caught:", e);
}
}
}
}
}
Try: MainListActivity.this instead of this.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainListActivity.this, android.R.layout.simple_list_item_1, blogPostTitles);
The first parameter of the ArrayAdapter constructor that you're using is a Context object, in the context were you create the ArrayAdapter, this is a MainListActivity.GetBlogPostsTask object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With