问题描述
我在运行此代码时遇到此错误,我在跟踪树屋构建博客阅读器android应用,现在我出现此错误
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
现在我在这段代码中出错了
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);
}
}
}
}
}
推荐答案
尝试:MainListActivity.this
而不是this
.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainListActivity.this, android.R.layout.simple_list_item_1, blogPostTitles);
正在使用的ArrayAdapter
构造函数的第一个参数是Context
对象,在上下文中是创建ArrayAdapter
的对象,this
是MainListActivity.GetBlogPostsTask
对象.
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.
这篇关于找不到适用于ArrayAdapter(MainListActivity.GetBlogPostsTask,int,String [])的合适的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!