Android的奇怪的错误 [英] Android strange error

查看:130
本文介绍了Android的奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个列表视图的应用程序,然后创建一个新的与片段,并要实现列表视图支离破碎。但是当我做我得到一个奇怪的错误。

 公共类片段1片段扩展{
 私人的ArrayList< FeedItem> feedList = NULL;
 私人进度进度= NULL;
 私人的ListView feedListView = NULL;  @覆盖
  公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
      查看rootView = inflater.inflate(R.layout.list_row_layout,集装箱,FALSE);      进度=(进度)rootView.findViewById(R.id.progressBar);
      字符串URL =;
      新DownloadFilesTask()执行(URL);
    返回rootView;
  }  公共无效updateList(){
      。feedListView =(ListView控件)getActivity()findViewById(R.id.custom_list);
      feedListView.setVisibility(View.VISIBLE);
      progressbar.setVisibility(View.GONE);      ** feedListView.setAdapter(新CustomListAdapter(这一点,feedList)); **
      feedListView.setOnItemClickListener(新OnItemClickListener(){              @覆盖
              公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
                      对象o = feedListView.getItemAtPosition(位置);
                      FeedItem newsData =(FeedItem)O;                      **意向意图=新意图(FeedListActivity.this,FeedDetailsActivity.class)**
                      intent.putExtra(饲料,newsData);
                      startActivity(意向);
              }
         });
           }             公共类DownloadFilesTask扩展的AsyncTask<字符串,整数,太虚> {      @覆盖
      保护无效onProgressUpdate(整数...值){
      }      @覆盖
      保护无效onPostExecute(虚空结果){
              如果(NULL!= feedList){
                      updateList();
              }
      }      @覆盖
      保护无效doInBackground(字符串... PARAMS){
              字符串URL =参数[0];              // URL从获取JSON字符串
              JSONObject的JSON = getJSONFromUrl(URL);              //解析JSON数据
              parseJson(JSON);
              返回null;
          }
          }
           公众的JSONObject getJSONFromUrl(字符串URL){
          InputStream为= NULL;
        JSONObject的jObj = NULL;
           JSON字符串= NULL;       //使HTTP请求
       尝试{
              // defaultHttpClient
              DefaultHttpClient的HttpClient =新DefaultHttpClient();
              HttpPost httpPost =新HttpPost(URL);              HTT presponse HTT presponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = HTT presponse.getEntity();
              是= httpEntity.getContent();              读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                              是,ISO-8859-1),8);
              StringBuilder的SB =新的StringBuilder();
              串线= NULL;
              而((行= reader.readLine())!= NULL){
                      sb.append(行+\\ n);
              }
              is.close();
              JSON = sb.toString();
      }赶上(UnsupportedEncodingException五){
              e.printStackTrace();
      }赶上(ClientProtocolException E){
              e.printStackTrace();
      }赶上(IOException异常五){
              e.printStackTrace();
      }         尝试{
              jObj =新的JSONObject(JSON);
           }赶上(JSONException E){
              Log.e(JSON解析器,错误分析数据+ e.toString());
         }             //返回JSON字符串
           返回jObj;         }             公共无效parseJson(JSON的JSONObject){
                 尝试{              //解析JSON对象
              如果(json.getString(身份)。equalsIgnoreCase(OK)){
                      JSONArray帖= json.getJSONArray(上岗);                      feedList =新的ArrayList< FeedItem>();                      的for(int i = 0; I< posts.length();我++){
                              JSONObject的岗位=(JSONObject的)posts.getJSONObject(I)
                              FeedItem项目=新FeedItem();
                              item.setTitle(post.getString(标题));
                              item.setDate(post.getString(说明));
                              item.setId(post.getString(ID));
                              item.setUrl(post.getString(URL));
                              item.setContent(post.getString(说明));
                              JSONArray附件= post.getJSONArray(附件);                              如果(零=附件&放大器;!&放大器; attachments.length()大于0){
                                      JSONObject的附件= attachments.getJSONObject(0);
                                      如果(附件!= NULL)
                                              item.setAttachmentUrl(attachment.getString(URL));
                              }                              feedList.add(项目);
                      }
              }
      }赶上(JSONException E){
              e.printStackTrace();
      }
     }     }

问题是在这行

  feedListView.setAdapter(新CustomListAdapter(这一点,feedList));
            意向意图=新意图(FeedListActivity.this,FeedDetailsActivity.class);

在该行多个标记
     - 行断点:片段1 [行:57] - updateList()

   - 构造CustomListAdapter(片段1,ArrayList的< FeedItem>)的
 未定义
      类型FeedListActivity没有外围实例是在范围访问

CustomListAdapter:

 公共类CustomListAdapter延伸BaseAdapter {        私人的ArrayList< FeedItem>的ListData;       私人LayoutInflater layoutInflater;        私人语境mContext;       公共CustomListAdapter(上下文的背景下,ArrayList的< FeedItem>的ListData){
            this.listData =的ListData;
            layoutInflater = LayoutInflater.from(上下文);
            mContext =背景;
             }         @覆盖
         公众诠释的getCount(){
            返回listData.size();
           }                  @覆盖
             公共对象的getItem(INT位置){
            返回listData.get(位置);
           }              @覆盖
              众长getItemId(INT位置){
            返回的位置;
          }            公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            ViewHolder持有人;
            如果(convertView == NULL){
                    convertView = layoutInflater.inflate(R.layout.list_row_layout,NULL);
                    持有人=新ViewHolder();
                    holder.headlineView =(TextView中)convertView.findViewById(R.id.title);
                    holder.reportedDateView =(TextView中)convertView.findViewById(R.id.date);
                    holder.imageView =(ImageView的)convertView.findViewById(R.id.thumbImage);
                    convertView.setTag(保持器);
            }其他{
                    支架=(ViewHolder)convertView.getTag();
            }            FeedItem newsItem =(FeedItem)listData.get(位置);
            holder.headlineView.setText(newsItem.getTitle());
            holder.reportedDateView.setText(newsItem.getDate());            如果(holder.imageView!= NULL){
                    新ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
            }            返回convertView;
      }       静态类ViewHolder {
            TextView的headlineView;
            TextView的reportedDateView;
            ImageView的ImageView的;
          }
             }


解决方案

在此行 feedListView.setAdapter(新CustomListAdapter(这一点,feedList)); 只是替换此 getActivity();

I have made an listview application, then i created a new one with fragments and want to implement listview to fragments. But when i do i got an strange error.

    public class Fragment1 extends Fragment {
 private ArrayList<FeedItem> feedList = null;
 private ProgressBar progressbar = null;
 private ListView feedListView = null;

  @Override  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
      View rootView = inflater.inflate(R.layout.list_row_layout, container, false);

      progressbar = (ProgressBar)rootView.findViewById(R.id.progressBar);
      String url = "";
      new DownloadFilesTask().execute(url);
    return rootView;


  } 

  public void updateList() {
      feedListView= (ListView)getActivity().findViewById(R.id.custom_list);
      feedListView.setVisibility(View.VISIBLE);
      progressbar.setVisibility(View.GONE);

      **feedListView.setAdapter(new CustomListAdapter(this, feedList));**
      feedListView.setOnItemClickListener(new OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                      Object o = feedListView.getItemAtPosition(position);
                      FeedItem newsData = (FeedItem) o;



                      **Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);**
                      intent.putExtra("feed", newsData);
                      startActivity(intent);
              }
         });
           }

             public class DownloadFilesTask extends AsyncTask<String, Integer, Void> {

      @Override
      protected void onProgressUpdate(Integer... values) {
      }

      @Override
      protected void onPostExecute(Void result) {
              if (null != feedList) {
                      updateList();
              }
      }

      @Override
      protected Void doInBackground(String... params) {
              String url = params[0];

              // getting JSON string from URL
              JSONObject json = getJSONFromUrl(url);

              //parsing json data
              parseJson(json);
              return null;
          }
          }


           public JSONObject getJSONFromUrl(String url) {
          InputStream is = null;
        JSONObject jObj = null;
           String json = null;

       // Making HTTP request
       try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);

              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();

              BufferedReader reader = new BufferedReader(new InputStreamReader(
                              is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
      } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
      } catch (ClientProtocolException e) {
              e.printStackTrace();
      } catch (IOException e) {
              e.printStackTrace();
      }

         try {
              jObj = new JSONObject(json);
           } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
         }

             // return JSON String
           return jObj;

         }

             public void parseJson(JSONObject json) {
                 try {

              // parsing json object
              if (json.getString("status").equalsIgnoreCase("ok")) {
                      JSONArray posts = json.getJSONArray("posts");

                      feedList = new ArrayList<FeedItem>();

                      for (int i = 0; i < posts.length(); i++) {
                              JSONObject post = (JSONObject) posts.getJSONObject(i);
                              FeedItem item = new FeedItem();
                              item.setTitle(post.getString("title"));
                              item.setDate(post.getString("description"));
                              item.setId(post.getString("id"));
                              item.setUrl(post.getString("url"));
                              item.setContent(post.getString("description"));
                              JSONArray attachments = post.getJSONArray("attachments");

                              if (null != attachments && attachments.length() > 0) {
                                      JSONObject attachment = attachments.getJSONObject(0);
                                      if (attachment != null)
                                              item.setAttachmentUrl(attachment.getString("url"));
                              }

                              feedList.add(item);
                      }
              }
      } catch (JSONException e) {
              e.printStackTrace();
      }
     }

     }

Problems are on this lines

          feedListView.setAdapter(new CustomListAdapter(this, feedList));
            Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);

Multiple markers at this line - Line breakpoint:Fragment1 [line: 57] - updateList()

- The constructor CustomListAdapter(Fragment1, ArrayList<FeedItem>) is 
 undefined
      No enclosing instance of the type FeedListActivity is accessible in scope

CustomListAdapter:

          public class CustomListAdapter extends BaseAdapter {

        private ArrayList<FeedItem> listData;

       private LayoutInflater layoutInflater;

        private Context mContext;

       public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
            this.listData = listData;
            layoutInflater = LayoutInflater.from(context);
            mContext = context;
             }

         @Override
         public int getCount() {
            return listData.size();
           }

                  @Override
             public Object getItem(int position) {
            return listData.get(position);
           }

              @Override
              public long getItemId(int position) {
            return position;
          }

            public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                    convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
                    holder = new ViewHolder();
                    holder.headlineView = (TextView) convertView.findViewById(R.id.title);
                    holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
                    holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
                    convertView.setTag(holder);
            } else {
                    holder = (ViewHolder) convertView.getTag();
            }

            FeedItem newsItem = (FeedItem) listData.get(position);
            holder.headlineView.setText(newsItem.getTitle());
            holder.reportedDateView.setText(newsItem.getDate());

            if (holder.imageView != null) {
                    new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
            }

            return convertView;
      }

       static class ViewHolder {
            TextView headlineView;
            TextView reportedDateView;
            ImageView imageView;
          }
             } 

解决方案

in this line feedListView.setAdapter(new CustomListAdapter(this, feedList)); just replace this with getActivity();

这篇关于Android的奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆