无尽的适配器数据在android中附加崩溃

时间:2012-03-07 10:16:40

标签: android android-layout

我在我的应用程序中使用Endless适配器,最初只有25个数据显示在列表视图中,当它到达结束时我需要追加下一个25个对象。

为此我通过将Endless适配器创建为单独的类并使用以下方式初始化适配器来使用它:

public BikeEndlessAdapter(Context c, ArrayList<BikeSearchResultsIdentifier> results,Activity test, String intentData23) {

    super(getAdapter(c,
            R.layout.search_bike, results, test));
    re=c;
    intentData=intentData23;

    ter=test.getLayoutInflater();   
      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);
    // TODO Auto-generated constructor stub
}
 static ListAdapter getAdapter(Context c, int searchBike,
        ArrayList<BikeSearchResultsIdentifier> results, Activity test) {
    // TODO Auto-generated method stub
        bikeItemList=results;
    adapter=new BikeListAdapter(c,
            R.layout.search_bike, bikeItemList, test);
    return adapter;
}

其中BikeListadapter是另一个ArrayList适配器类,因为每一行都有不同的属性。

第一次使用Endless适配器加载数据时,使用以下代码成功地在列表中显示了第25个dataObject,

        final ListView bikeListView = (ListView) findViewById(R.id.list_bike);

    bikeListView.setAdapter(new BikeEndlessAdapter(BikesList.this, bikeItemList, this,intentData));

在我的BikeList类中。

当它到达listview的末尾(即第25行)时,显示了相应的无限适配器动画,然后执行了cacheInBackground方法,在那个方法中我点击了服务提取下一组add也成功完成了。

但是当我尝试追加它时会抛出错误,

@Override
protected void appendCachedData() {
    // TODO Auto-generated method stub

    //@SuppressWarnings("unchecked")
    ArrayAdapter<BikeSearchResultsIdentifier> adapter=(ArrayAdapter<BikeSearchResultsIdentifier>)getWrappedAdapter();
    System.out.println("Wrapper class: "+adapter);
    for(int i=0;i<bikeItemList.size();i++){
        adapter.add(bikeItemList.get(i));
    }
    adapter.notifyDataSetChanged();
}

这是我有的日志错误:

   03-07 15:52:02.292: E/AndroidRuntime(762): java.lang.NullPointerException
03-07 15:52:02.292: E/AndroidRuntime(762):  at com.InternetGMBH.ThousandPS.Activities.BikeEndlessAdapter.appendCachedData(BikeEndlessAdapter.java:93)
03-07 15:52:02.292: E/AndroidRuntime(762):  at com.commonsware.cwac.endless.EndlessAdapter$AppendTask.onPostExecute(EndlessAdapter.java:179)
03-07 15:52:02.292: E/AndroidRuntime(762):  at com.commonsware.cwac.endless.EndlessAdapter$AppendTask.onPostExecute(EndlessAdapter.java:1)
03-07 15:52:02.292: E/AndroidRuntime(762):  at android.os.AsyncTask.finish(AsyncTask.java:417)
03-07 15:52:02.292: E/AndroidRuntime(762):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-07 15:52:02.292: E/AndroidRuntime(762):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-07 15:52:02.292: E/AndroidRuntime(762):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 15:52:02.292: E/AndroidRuntime(762):  at android.os.Looper.loop(Looper.java:123)
03-07 15:52:02.292: E/AndroidRuntime(762):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-07 15:52:02.292: E/AndroidRuntime(762):  at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:52:02.292: E/AndroidRuntime(762):  at java.lang.reflect.Method.invoke(Method.java:521)
03-07 15:52:02.292: E/AndroidRuntime(762):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-07 15:52:02.292: E/AndroidRuntime(762):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-07 15:52:02.292: E/AndroidRuntime(762):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我在列表视图中获取和设置值时发现问题,我的BikeListAdapter中的问题,

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        **convertView = mInflater.inflate(R.layout.search_bike,null);**
        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.model=(TextView)convertView.findViewById(R.id.model);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.title.setText(bikeSearchResults.get(position).getBikeBrand());
    holder.model.setText(bikeSearchResults.get(position).getModel());

    return convertView;
}

表示convertView = mInflater.inflate(R.layout.search_bike,null);我在使用错误的上下文时出错了,然后我使用下面的代码来纠正它,

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.search_bike,null);

现在无限的自定义对象适配器工作正常。