运行此代码时出现此错误,我正在关注树屋Build a blog reader android app ,现在我收到此错误
错误:(120,52)错误:找不到适合ArrayAdapter的构造函数(MainListActivity.GetBlogPostsTask,int,String []) 构造函数ArrayAdapter.ArrayAdapter(Context,int,int)不适用 (参数不匹配; MainListActivity.GetBlogPostsTask无法转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int,String [])不适用 (参数不匹配; MainListActivity.GetBlogPostsTask无法转换为Context) 构造函数ArrayAdapter.ArrayAdapter(Context,int,List)不适用 (参数不匹配; MainListActivity.GetBlogPostsTask无法转换为Context)
现在我在这段代码中遇到错误
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);
}
}
}
为了便于理解,我复制了整个代码,以防万一我错过了一些东西,我正在按照教程进行操作,但是当我收到错误时,作者没有收到任何错误,可能是什么问题< / p>
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);
}
}
}
}
}
答案 0 :(得分:5)
尝试: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
对象。
答案 1 :(得分:2)
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, blogPostTitles)
编写此代码时,this
引用内部类。
上下文应该是活动类。
我建议你检查你的结束括号
答案 2 :(得分:1)
您已在内部班级udpateList()
中使用此方法GetBlogPostsTask
因此,第一个参数this
将不起作用
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, blogPostTitles);
删除this
,然后使用MainListActivity.this
。
这里使用的ArrayAdpater
类的重载构造函数应该是.. ArrayAdapter(Context context, int resource, T[] objects)
现在,Context
不能只是任何java类,它必须是Android系统特定的资源:
根据API docs:
这是一个抽象类,其实现由Android系统提供。它允许访问特定于应用程序的资源和类