这是我的演示适配器。
public class DemoAdapter extends EndlessAdapter {
private RotateAnimation rotate = null;
private View pendingView = null;
private ArrayList<ShipTo> next;
private static ArrayList<ShipTo> list;
private int pageFrom = 1;
private static Activity act;
DemoAdapter(Context ctxt, ArrayList<ShipTo> list) {
super(new Adapater());
act = (Activity) ctxt;
DemoAdapter.list = list;
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 = LayoutInflater.from(parent.getContext()).inflate(
R.layout.row, null);
pendingView = row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView = row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
startProgressAnimation();
return (row);
}
@Override
protected boolean cacheInBackground() {
// SystemClock.sleep(1000); // pretend to do work
pageFrom += 10;
next = ItemController.getInstance().getShipTo(pageFrom + "", "10", "0");
return (ItemController.moreShipTo.equalsIgnoreCase("1")) ? true : false;
}
@Override
protected void appendCachedData() {
if (next != null)
list.addAll(next);
if (!(ItemController.moreShipTo.equalsIgnoreCase("1")) ? true : false)
Toast.makeText(act, "No More Category found", Toast.LENGTH_SHORT)
.show();
}
void startProgressAnimation() {
if (pendingView != null) {
pendingView.startAnimation(rotate);
}
}
private static class Adapater extends BaseAdapter {
private class ViewHolder {
private TextView tv;
}
@Override
public int getCount() {
return list.size();
}
@Override
public ShipTo getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
try {
ViewHolder viewHolder = null;
ShipTo shipTo = list.get(arg0);
LayoutInflater lf = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// if (arg1 == null) {
viewHolder = new ViewHolder();
arg1 = lf.inflate(R.layout.category_spinner_textview, null);
viewHolder.tv = (TextView) arg1
.findViewById(R.id.textView1_cat_spinner);
arg1.setTag(viewHolder);
/*
* } else { viewHolder = (ViewHolder) arg1.getTag(); }
*/
viewHolder.tv.setText(shipTo.getName());
arg1.setTag(R.layout.category_spinner_textview, shipTo);
viewHolder.tv.setPadding(5, 10, 0, 10);
} catch (Exception e) {
e.printStackTrace();
}
return arg1;
}
}
}
这是我在对话框中使用无限适配器显示列表视图的活动。
public class EndlessAdapterDemo extends Activity {
public ArrayList<ShipTo> shipToList;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new Thread(new Runnable() {
@Override
public void run() {
shipToList = ItemController.getInstance().getShipTo(
"1", "10", "0");
runOnUiThread(new Runnable() {
@Override
public void run() {
showCategoryList();
}
});
}
}).start();
}
});
}
private void showCategoryList() {
final Dialog categoryDialog = new Dialog(this);
categoryDialog.setTitle("Select Category");
categoryDialog.setContentView(R.layout.custumdialoglstview);
ListView lv = (ListView) categoryDialog.findViewById(R.id.listView1);
DemoAdapter adapter = null;
if (adapter == null) {
adapter = new DemoAdapter(EndlessAdapterDemo.this, shipToList);
}
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println(arg1.getTag().getClass().getName());
ShipTo temp = (ShipTo) arg1
.getTag(R.layout.category_spinner_textview);
categoryDialog.dismiss();
}
});
categoryDialog.show();
}
}
当我运行上面的代码时,它会抛出非法状态异常或空指针异常。
以下是我的 logcat 详细信息
11-17 09:40:08.532: D/AndroidRuntime(331): Shutting down VM
11-17 09:40:08.532: W/dalvikvm(331): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-17 09:40:08.541: E/AndroidRuntime(331): FATAL EXCEPTION: main
11-17 09:40:08.541: E/AndroidRuntime(331): java.lang.NullPointerException
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.ListView.measureScrapChild(ListView.java:1135)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.ListView.measureHeightOfChildren(ListView.java:1218)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.ListView.onMeasure(ListView.java:1127)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.View.measure(View.java:8313)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.View.measure(View.java:8313)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.View.measure(View.java:8313)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.View.measure(View.java:8313)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.View.measure(View.java:8313)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.View.measure(View.java:8313)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.os.Looper.loop(Looper.java:123)
11-17 09:40:08.541: E/AndroidRuntime(331): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-17 09:40:08.541: E/AndroidRuntime(331): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 09:40:08.541: E/AndroidRuntime(331): at java.lang.reflect.Method.invoke(Method.java:507)
11-17 09:40:08.541: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-17 09:40:08.541: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-17 09:40:08.541: E/AndroidRuntime(331): at dalvik.system.NativeStart.main(Native Method)
可能是什么问题?