使用带有SimpleAdapter的EndlessAdapter的NullPointerException

时间:2010-03-25 22:47:41

标签: android endlessadapter

我使用commonsguy的EndlessAdapter和SimpleAdapter。我可以在没有问题的情况下向下滚动时加载数据,但是当我向上滚动时我有一个NullPointerException。问题在于方法

    @Override
 public View getView(int position, View convertView,ViewGroup parent) {
  return wrapped.getView(position, convertView, parent);
 }
来自班级AdapterWrapper

wrapped.getView(position, convertView,parent)的调用引发了异常,我不知道为什么。

这是我EndlessAdapter的实现:

//Inner class in SearchTextActivity
    class DemoAdapter extends EndlessAdapter {

      private RotateAnimation rotate = null;

      DemoAdapter(ArrayList<HashMap<String, String>> result) {
       super(new SimpleAdapter(SearchTracksActivity.this,
         result,
         R.layout.textlist_item,
         PROJECTION_COLUMNS,
         VIEW_MAPPINGS));

       rotate = new RotateAnimation( 0f,
         360f,
         Animation.RELATIVE_TO_SELF,
         0.5f,
         Animation.RELATIVE_TO_SELF,
         0.5f);
       rotate.setDuration(600);
       rotate.setRepeatMode(Animation.RESTART);
       rotate.setRepeatCount(Animation.INFINITE);
      }

      @Override
      protected View getPendingView(ViewGroup parent) {
       View row=getLayoutInflater().inflate(R.layout.textlist_item, null);

       View child=row.findViewById(R.id.title);
       child.setVisibility(View.GONE);

       child=row.findViewById(R.id.username);
       child.setVisibility(View.GONE);

       child=row.findViewById(R.id.throbber);
       child.setVisibility(View.VISIBLE);
       child.startAnimation(rotate);

       return row;
      }


      @Override
      @SuppressWarnings("unchecked")
      protected void rebindPendingView(int position, View row) {
       HashMap<String, String> res = (HashMap<String, String>)getWrappedAdapter().getItem(position);


       View child=row.findViewById(R.id.title);
       ((TextView)child).setText(res.get("title"));
       child.setVisibility(View.VISIBLE);    

       child=row.findViewById(R.id.username);
       ((TextView)child).setText(res.get("username"));
       child.setVisibility(View.VISIBLE);

       ImageView throbber=(ImageView)row.findViewById(R.id.throbber);
       throbber.setVisibility(View.GONE);
       throbber.clearAnimation();

      }

      boolean mFinal = true;

      @Override
      protected boolean cacheInBackground() {

       EditText searchText = (EditText)findViewById(R.id.searchText);
       String textToSearch = searchText.getText().toString();

       Util.getSc().searchText(textToSearch , offset, limit, new ResultListener<ArrayList<Text>>() {

        @Override
        public void onError(Exception e) {
         e.toString();
         mFinal = false;
        }

        @Override
        public void onSuccess(ArrayList<Text> result) {


         if(result.size() == 0){
          mFinal = false;
         }else{
          texts.addAll(result);

          offset++;
         }
        }
       });

       return mFinal;
      }

      @Override
      protected void appendCachedData() {

       for(Text text : texts){
        result.add(text.getMapValues());
       }
       texts.clear();
      }
     }

我用这种方式:

public class SearchTextActivity extends AbstractListActivity {

 private static final String[] PROJECTION_COLUMNS = new String[] {
  TextStore.Text.TITLE,
  TextStore.Text.USER_NAME};

 private static final int[] VIEW_MAPPINGS = new int[] {
  R.id.Text_title,
  R.id.Text_username};

 ArrayList<HashMap<String, String>> result;

 static ArrayList<Text> texts;
 static int offset = 0;
 static int limit = 1;

 @Override
 void onAbstractCreate(Bundle savedInstance) {
  setContentView(R.layout.search_tracks);
  setupViews();
 }

 private void setupViews() {

  ImageButton searchButton = (ImageButton)findViewById(R.id.searchButton);
  updateView();
 }

 SimpleAdapter adapter;

 void updateView(){
  if(result == null) {
   result = new ArrayList<HashMap<String, String>>();
  }

  if(tracks == null) {
   texts = new ArrayList<Text>();
  }
 }

 public void sendQuery(View v){
  offset = 0;
  texts.clear();
  result.clear();

  setListAdapter(new DemoAdapter(result));
 }
}

有谁知道会出现什么问题?

1 个答案:

答案 0 :(得分:1)

  

它在SimpleAdapter中。

我首先要删除EndlessAdapter,然后将您的列表放在SimpleAdapter上。

如果可行,那么EndlessAdapter在它所包装的适配器的工作方式中可能存在错误 - 在这种情况下,我需要一个演示错误的示例项目。

如果SimpleAdapter在没有EndlessAdapter的情况下失败,您需要更多地调查如何设置SimpleAdapter