找不到适合ArrayAdapter的构造函数(MainListActivity.GetBlogPostsTask,int,String )
我在运行此代码时遇到此错误,我正在关注树屋构建一个博客阅读器Android应用程序 ,现在我收到此错误
错误:(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 adapter = new ArrayAdapter(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 { @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 adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, blogPostTitles); setListAdapter(adapter); } catch (JSONException e) { Log.e(TAG, "Exception caught:", e); } } } } }
尝试: MainListActivity.this
而不是this
。
ArrayAdapter adapter = new ArrayAdapter (MainListActivity.this, android.R.layout.simple_list_item_1, blogPostTitles);
您正在使用的ArrayAdapter
构造函数的第一个参数是Context
对象,在您创建ArrayAdapter
的上下文中, this
是一个MainListActivity.GetBlogPostsTask
对象。
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, blogPostTitles)
当你编写这段代码时, this
引用了内部类。 上下文应该是活动类。 我建议你检查你的结束括号
你在你的内部类GetBlogPostsTask
了这个方法udpateList()
因此第一个参数在这里不起作用
new ArrayAdapter(this, android.R.layout.simple_list_item_1, blogPostTitles);
删除this
,然后使用MainListActivity.this
。
这里使用的ArrayAdpater
类的重载构造函数应该是.. ArrayAdapter(Context context, int resource, T[] objects)
现在, Context
不能只是任何java类,它需要是Android系统特定的资源:
根据API文档 :
这是一个抽象类,其实现由Android系统提供。 它允许访问特定于应用程序的资源和类