2017-02-19 75 views
0

我有一個片段類,其中我使用asynctask加載列表視圖數據。 一旦用戶到達滾動結束,我需要加載另一組數據,我正在做這件事。應用程序崩潰時,無盡的列表視圖是滾動

listView.setOnScrollListener(new AbsListView.OnScrollListener() { 

      @Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 
      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

       if (userListAdapter == null) 
        return; 

       if (userListAdapter.getCount() == 0) 
        return; 

       int l = visibleItemCount + firstVisibleItem; 
       if (l >= totalItemCount && !isLoading) { 
        // It is time to add new data. We call the listener 
        listView.addFooterView(footer); 
        isLoading = true; 
        listener.loadData(); 
       } 
      } 
     }); 

我loaddata得到回調這裏

@Override 
    public void loadData() { 
     if (connection != null) { 
      connection.getmService().new FetchJsonData().execute(Constants.WEEK_URL1); 
     } 

    } 

我的適配器代碼。

public class UserListAdapter extends BaseAdapter { 

    private LayoutInflater layoutInflater; 
    private ArrayList<UserInfoModel> userInfoModelArrayList; 

    public UserListAdapter(Context context, ArrayList<UserInfoModel> userInfoModelArrayList) { 
     this.userInfoModelArrayList = userInfoModelArrayList; 
     layoutInflater = LayoutInflater.from(context); 
    } 

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

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 
     if (null == convertView) { 
      convertView = layoutInflater.inflate(R.layout.list_row, parent,false); 
      viewHolder = new ViewHolder(); 
      viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view); 
      viewHolder.userName = (TextView) convertView.findViewById(R.id.user_name); 
      viewHolder.userID = (TextView) convertView.findViewById(R.id.userID); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 
     viewHolder.userName.setText(userInfoModelArrayList.get(position).getUserName()); 
     viewHolder.userID.setText(userInfoModelArrayList.get(position).getUserID()); 
     return convertView; 
    } 


    public void setFilter(ArrayList<UserInfoModel> informationArrayList) { 
     userInfoModelArrayList = new ArrayList<>(); 
     userInfoModelArrayList.addAll(informationArrayList); 
     notifyDataSetChanged(); 

    } 

    public void setData(ArrayList<UserInfoModel> informationArrayList) { 
     userInfoModelArrayList.addAll(informationArrayList); 
     notifyDataSetChanged(); 
    } 


    private static class ViewHolder { 
     ImageView imageView; 
     TextView userName; 
     TextView userID; 
    } 
} 

但我的應用程序崩潰,一旦達到第一套我已經在列表視圖的頁腳添加進度數據。

錯誤日誌

02-19 18:45:09.757 3114-3114/example.service.android.santosh.com.androidservice E/AndroidRuntime: FATAL EXCEPTION: main 
                            Process: example.service.android.santosh.com.androidservice, PID: 3114 
                            java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference 
                             at android.widget.AbsListView.obtainView(AbsListView.java:2363) 
                             at android.widget.ListView.makeAndAddView(ListView.java:1876) 
                             at android.widget.ListView.fillDown(ListView.java:702) 
                             at android.widget.ListView.fillGap(ListView.java:666) 
                             at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5060) 
                             at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4608) 
                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                             at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                             at android.view.Choreographer.doFrame(Choreographer.java:603) 
                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                             at android.os.Handler.handleCallback(Handler.java:746) 
                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                             at android.os.Looper.loop(Looper.java:148) 
                             at android.app.ActivityThread.main(ActivityThread.java:5443) 
                             at java.lang.reflect.Method.invoke(Native Method) 
                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

請幫助謝謝:)

+0

你可以請張貼堆棧跟蹤以及。 ?這對確定問題將有很大幫助。 –

+0

當然我會給我一個 –

+0

請張貼與getImportantForAccessibility方法相關的代碼,即如何調用它和方法本身。 –

回答

0

我已經解決了這個問題與adpater無關,這是由於在listview頁腳視圖的滾動中添加頁腳視圖應該添加在片段的創建上。

的onCreate

View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loading_view, null, false); 
     listView.addFooterView(footerView); 

onScroll

if (l >= totalItemCount && !isLoading) { 
        // It is time to add new data. We call the listener 
        isLoading = true; 
        listener.loadData(); 
       } 

它解決了我的問題。順便說一句,謝謝大家:)

+0

啊!對,你在footScroll()中附加了footerView。達姆,我不應該錯過這一點。無論如何,感謝您的信息。 –

0

您是否嘗試過這樣的事情...

MyViewHolder viewHolder = null; 

if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     viewHolder = new MyViewHolder();; 
     convertView = inflater.inflate(R.layout.all_brands_layout_search, null); 
     viewHolder.imageOne = (DynamicHeightImageView) convertView.findViewById(R.id.my_brand_pinrest_view); 
     convertView.setTag(viewHolder); 
    }else{ 
viewHolder= (MyViewHolder)convertView.getTag(); 
} 

這應該是寫一個適配器的理想方式。

+0

不幫助兄弟仍然同樣的崩潰。 –

+0

看來你的getCount()或getItemId(int pos)有問題。你能否發佈適配器代碼以獲得更多清晰的內容。 ? –

+0

已添加完整的適配器代碼。 –

相關問題