我在运行此代码时遇到此错误,我正在关注 treehouse Build a blog reader android app 现在我收到了这个错误
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)
现在我在这段代码中遇到错误
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);
}
}
}
为了便于理解,我复制了整个代码,以防万一我遗漏了什么,我正在按照教程进行操作,而作者在我遇到错误时没有遇到任何错误,可能是什么问题
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
对象。
关于java - 找不到适合 ArrayAdapter(MainListActivity.GetBlogPostsTask,int,String[]) 的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32350275/