2010-03-25 4 views
0

SimpleAdapter와 함께 commonsguy에서 EndlessAdapter를 사용하고 있습니다. 문제없이 스크롤을 만들 때 데이터를로드 할 수 있지만 스크롤을 위로 할 때 NullPointerException이 발생합니다. 문제는 방법에 있습니다SimpleAdapter에서 EndlessAdapter를 사용하는 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)); 
} 
} 

사람이 문제가 될 수 알고 있습니까?

+0

EndlessAdapter로 SimpleAdapter를 시도하지 않았습니다. 'NullPointerException'가'wrapped.getView (position, convertView, parent)'행에 직접 있으면'wrapped'는'null'이어야합니다. – CommonsWare

+0

답장을 보내 주셔서 감사합니다.하지만 '포장 됨'이 null이 아닌지 확인했습니다. 이 오류는 wrapped.getView (position, convertView, parent)에서 유래합니다. SimpleAdapter 안에 있습니다. 어쩌면 인덱스 '위치'와 관련된 문제이지만이 위치에 대한보기가 있습니다. 거기에서 무슨 일이 일어나고 있는지 모르겠습니다. – ahmontero

답변

1

SimpleAdapter 안에 있습니다.

은 내가 EndlessAdapter 치우는에 의해 시작하고 당신의 목록은 SimpleAdapter에있을 것이다.

작동하는 경우 EndlessAdapter이 랩핑하는 어댑터와 작동하는 방식에 버그가있을 수 있습니다.이 경우 오류를 보여주는 샘플 프로젝트가 필요합니다.

SimpleAdapterEndlessAdapter없이 실패하는 경우 SimpleAdapter을 어떻게 설정하는지 더 자세히 조사해야합니다.

+0

테스트를 통해 오류를 다시 만들었습니다. SimpleAdpater를 사용하면 모든 것이 정상이지만 SimpleAdapter와 함께 EndlessAdapter를 사용하면 설명하기 전에 문제가 발생합니다. Eclipse에서 데모 프로젝트를 만들었습니다. http://bit.ly/alTapV 도움을 주셔서 감사합니다. – ahmontero

+0

'EndlessAdapter'는 곧'SimpleAdapter'와 함께 작동하지 않습니다. 'SimpleAdapter'라는 마침표는 추천하지 않습니다. 'SimpleAdapter'가'ArrayAdapter'가 더 효율적으로 해결할 수 없다는 문제를 해결했습니다 (적은 데이터 복사 = 적은 CPU 시간, 적은 가비지 콜렉션 등). – CommonsWare

+0

알겠습니다. 이해합니다. 그런 다음 ArrayAdapter를 사용하려고합니다. 시간 내 줘서 고마워. – ahmontero