移动后报空值的错误 不太理解 求指点迷经。要求前4行ListView的图片亮 4行以下变灰色 程序进入的 时候 还可以完成效果 但是把下面打移动下去就报空值。
public class SetScene extends ListActivity implements OnClickListener {
private MyAdapter adapter = null;
private Button back, but_ci_del, but_ci_s, but_ci_select;
private ArrayList<Map<String, Object>> array;
private Context context;
private TextView tv_title;
private ListViewInterceptor tlv;
private String schemename, schemenames, sid; protected String Snames = "";
private Map<String, Integer> iamgel = new HashMap<String, Integer>(); // 启动
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.setscene);
context = SetScene.this;
// 获取数据
sid = SaveSharedPreferences.getSceneMode(SetScene.this);
// 通过sid 查询m
MenuSchemeDao msd = new MenuSchemeDao(context);
// 需要显示的 场景的id Log.i("csxm", msd.getMenuids(sid));
tv_title = (TextView) findViewById(R.id.tv_titles);
but_ci_del = (Button) findViewById(R.id.but_ci_del);
but_ci_s = (Button) findViewById(R.id.but_ci_s);
but_ci_select = (Button) findViewById(R.id.but_ci_select);
but_ci_del.setOnClickListener(this);
but_ci_s.setOnClickListener(this);
but_ci_select.setOnClickListener(this); // 通过sid 查询m
MySqlHelper sql = new MySqlHelper(context);
Cursor name = sql.fetchMenuSchemeitem(sid);
schemename = name.getString(name.getColumnIndex("schemename"));
System.out.println(schemename);
tv_title.setText(schemename); name.close();
sql.close(); array = getData();
adapter = new MyAdapter(array);
setListAdapter(adapter); back = (Button) findViewById(R.id.back);
back.setOnClickListener(this); tlv = (ListViewInterceptor) getListView();
tlv.setDropListener(onDrop);
tlv.getAdapter();
tlv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Class<Scenese> intclass = Scenese.class;
Intent localIntent = new Intent();
Bundle localBundle = new Bundle();
String titles;
array.get(arg2);
titles = (String) array.get(arg2).get("title");
localBundle.putString("titles", "" + titles);

// 判断然后跳转
localIntent.putExtras(localBundle);
localIntent.setClass(SetScene.this, intclass);
startActivity(localIntent);
}
});
} // 滑动特效 private ListViewInterceptor.DropListener onDrop = new ListViewInterceptor.DropListener() { public void drop(int from, int to) {
Map item = adapter.getItem(from);
adapter.remove(item);
adapter.insert(item, to);
StringBuilder Sname = new StringBuilder(); for (int i = 0; i < adapter.getCount(); i++) {
adapter.getItem(i).put("flag", "0");
adapter.getItem(i).put("image_id", "3");
array.set(i, adapter.getItem(i));
array.get(3).put("flag", "1");
array.get(0).put("image_id", "2");
array.get(1).put("image_id", "2");
array.get(2).put("image_id", "2");
array.get(3).put("image_id", "2"); int a = 0;
String sname = (String) array.get(i).get("title");
MenuSchemeDao menudao = new MenuSchemeDao(context);
Cursor cursor = menudao.getSnameId(sname); while (cursor.moveToNext()) {
a = cursor.getInt(cursor.getColumnIndex("_id"));
} Sname.append(String.valueOf(a) + ",");
cursor.close();
menudao.close(); Log.e("a", array.get(i).get("image_id").toString() + ";" + to + ";"
+ array.get(i).get("title")); }
// 让第四行 显示分割
schemenames = Sname.toString();
System.out.println(schemenames); array.get(0).put("image_id", "2");
adapter.chargeProgress(0, array.get(0));
array.get(1).put("image_id", "2");
adapter.chargeProgress(1, array.get(1));
array.get(2).put("image_id", "2");
adapter.chargeProgress(2, array.get(2));
array.get(3).put("image_id", "2");
adapter.chargeProgress(3, array.get(3)); array.get(3).put("flag", "1");
adapter.chargeProgress(3, array.get(3));
Log.i("a", array.get(3).get("image_id").toString() + ";" + to + ";"
+ array.get(3).get("title"));
} }; class MyAdapter extends ArrayAdapter<Map<String, Object>> {
ArrayList<Map<String, Object>> list; MyAdapter(ArrayList<Map<String, Object>> array) {
super(SetScene.this, R.layout.setsceneitem, array); list = array;
} public ArrayList<Map<String, Object>> getList() {
return list;
} // 改变进度,postion就是要改的那个进度
public void chargeProgress(int postion, Map<String, Object> detail) {
list.set(postion, detail);
System.out.println("charged:" + detail.get("flag"));
notifyDataSetChanged();
} public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.setsceneitem, parent, false);
}
TextView label = (TextView) row.findViewById(R.id.tv_scheme);
ImageView imageView = (ImageView) row.findViewById(R.id.iv_icon);
RelativeLayout line = (RelativeLayout) row.findViewById(R.id.line); Map<String, Object> detail = list.get(position); if (detail.get("image_id").toString() == "2") {
label.setText(detail.get("title").toString());
label.setTextColor(getResources().getColor(R.color.textColor));
//这里报错!
imageView.setImageResource(Integer.valueOf(detail.get("image_icon")
.toString()));
} else if (detail.get("image_id").toString() == "3") {
label.setText(detail.get("title").toString());
label.setTextColor(getResources().getColor(R.color.textColor1));
System.out.println(Integer
.valueOf(detail.get("image_scene").toString()));
imageView.setImageResource(Integer.valueOf(detail.get("image_scene")
.toString()));
}
if (detail.get("flag").toString() == "1") {
line.setVisibility(0);
} else {
line.setVisibility(8);
}
row.postInvalidate();
return (row);
}
} private ArrayList<Map<String, Object>> getData() { ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>(); MenuSchemeDao menudao = new MenuSchemeDao(context);
Cursor cursor = menudao.getMenuid(schemename); iamgel.put("icon_meeting.png", Integer.valueOf(R.drawable.icon_meeting));
iamgel.put("icon_nocall.png", Integer.valueOf(R.drawable.icon_nocall));
iamgel.put("icon_blacklist.png",
Integer.valueOf(R.drawable.icon_blacklist));
iamgel.put("icon_callmsg.png", Integer.valueOf(R.drawable.icon_callmsg));
iamgel.put("icon_car.png", Integer.valueOf(R.drawable.icon_car));
iamgel.put("icon_sport.png", Integer.valueOf(R.drawable.icon_sport)); iamgel.put("icon_rest.png", Integer.valueOf(R.drawable.icon_rest));
iamgel.put("icon_class.png", Integer.valueOf(R.drawable.icon_class));
iamgel.put("icon_study.png", Integer.valueOf(R.drawable.icon_study));
iamgel.put("icon_nobattery.png",
Integer.valueOf(R.drawable.icon_nobattery));
iamgel.put("icon_travel.png", Integer.valueOf(R.drawable.icon_travel));
iamgel.put("icon_train.png", Integer.valueOf(R.drawable.icon_train)); iamgel.put("scene_meeting.png",
Integer.valueOf(R.drawable.scene_meeting));
iamgel.put("scene_nocall.png", Integer.valueOf(R.drawable.scene_nocall));
iamgel.put("scene_blacklist.png",
Integer.valueOf(R.drawable.scene_blacklist));
iamgel.put("scene_callmsg.png",
Integer.valueOf(R.drawable.scene_callmsg));
iamgel.put("scene_car.png", Integer.valueOf(R.drawable.scene_car));
iamgel.put("scene_sport.png", Integer.valueOf(R.drawable.scene_sport)); iamgel.put("scene_rest.png", Integer.valueOf(R.drawable.scene_rest));
iamgel.put("scene_class.png", Integer.valueOf(R.drawable.scene_class));
iamgel.put("scene_study.png", Integer.valueOf(R.drawable.scene_study));
iamgel.put("scene_nobattery.png",
Integer.valueOf(R.drawable.scene_nobattery));
iamgel.put("scene_travel.png", Integer.valueOf(R.drawable.scene_travel));
iamgel.put("scene_train.png", Integer.valueOf(R.drawable.scene_train)); while (cursor.moveToNext()) {
SetSceneInfo info = new SetSceneInfo();
info.menuids = cursor.getString(cursor.getColumnIndex("menuids"));
String menuids = info.getMenuids();
String[] ids = menuids.split(","); for (int i = 0; i < ids.length; i++) {
Cursor mcursor = menudao.getMenuName(ids[i]);
info.schemename = mcursor.getString(mcursor
.getColumnIndex("menuname"));
info.image = mcursor.getString(mcursor
.getColumnIndex("iconfile"));
String imname = info.getImage();
String schemename = info.getSchemename(); map = new HashMap<String, Object>(); if (i <= 3) {
map.put("image_id", "2");
map.put("title", schemename);
map.put("image_icon", iamgel.get(imname));
} else {
String imagename = "scene" + imname.substring(4);
map.put("image_id", "3");
map.put("title", schemename);
map.put("image_scene", iamgel.get(imagename)); }
if (i == 3) {
map.put("flag", "1");
} else {
map.put("flag", "0");
}
Log.d("tag", schemename);
list.add(map);
mcursor.close();
}
}
cursor.close();
menudao.close();
return list;
} public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.back:
finish();
break;
case R.id.but_ci_del:
finish();
break;
case R.id.but_ci_s:
finish();
break;
case R.id.but_ci_select:
MenuSchemeDao msdao = new MenuSchemeDao(context);
msdao.updateMenuids(schemenames, sid);
msdao.close();
System.out.print(sid);
System.out.println(schemenames);
finish();
break;
}
}
}

解决方案 »

  1.   

    // 滑动特效
    package com.eastcom.intelligentanswer.adapter;import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.PixelFormat;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewConfiguration;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.ListView;
    import com.eastcom.intelligentanswer.R;
    import com.eastcom.intelligentanswer.util.DensityUtil;public class ListViewInterceptor extends ListView { private DropListener mDropListener; private ImageView mDragView;
    private int mDragPos; // which item is being dragged
    private int mFirstDragPos; // where was the dragged item originally
    private int mDragPoint; // at what offset inside the item did the user grab
    // it
    private int mCoordOffset; // the difference between screen coordinates and
    // coordinates in this view private Rect mTempRect = new Rect();
    private final int mTouchSlop;
    private int mHeight;
    @SuppressWarnings("unused")
    private int mUpperBound;
    @SuppressWarnings("unused")
    private int mLowerBound;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowParams;
    private int dragndropBackgroundColor = 0x00000000;
    private Bitmap mDragBitmap;
    private int mItemHeightHalf = 60;
    private int mItemHeightNormal = 60;
    private int mItemHeightExpanded = 128;
    Context c; public ListViewInterceptor(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    c = context;
    // TODO Auto-generated constructor stub
    } public ListViewInterceptor(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    c = context;
    // TODO Auto-generated constructor stub
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    } @Override
    public boolean onTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    if ((mDropListener != null) && mDragView != null) {
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
    Rect r = mTempRect;
    mDragView.getDrawingRect(r);
    stopDragging();
    if (mDropListener != null && mDragPos >= 0
    && mDragPos < getCount()) {
    mDropListener.drop(mFirstDragPos, mDragPos);
    }
    unExpandViews(false);
    break; case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
    int x = (int) ev.getX();
    int y = (int) ev.getY();
    dragView(x, y); int itemnum = getItemForPosition(y);
    if (itemnum >= 0) {
    if (action == MotionEvent.ACTION_DOWN
    || itemnum != mDragPos) { mDragPos = itemnum;
    doExpansion();
    }
    }
    break;
    }
    return true;
    }
    return super.onTouchEvent(ev); } @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    if (mDropListener != null) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
    int x = (int) ev.getX();
    int y = (int) ev.getY();
    int itemnum = pointToPosition(x, y);
    if (itemnum == AdapterView.INVALID_POSITION) {
    break;
    }
    ViewGroup item = (ViewGroup) getChildAt(itemnum
    - getFirstVisiblePosition());
    mDragPoint = y - item.getTop();
    mCoordOffset = ((int) ev.getRawY()) - y;
    View dragger = item.findViewById(R.id.iv1);
    Rect r = mTempRect;
    // dragger.getDrawingRect(r); r.left = dragger.getLeft();
    r.right = dragger.getRight();
    r.top = dragger.getTop();
    r.bottom = dragger.getBottom(); if ((r.left < x) && (x < r.right)) {
    item.setDrawingCacheEnabled(true);
    // Create a copy of the drawing cache so that it does not
    // get recycled
    // by the framework when the list tries to clean up memory
    Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
    startDragging(bitmap, y);
    mDragPos = itemnum;
    mFirstDragPos = mDragPos;
    mHeight = getHeight();
    int touchSlop = mTouchSlop;
    mUpperBound = Math.min(y - touchSlop, mHeight / 3);
    mLowerBound = Math.max(y + touchSlop, mHeight * 2 / 3);
    return false;
    }
    mDragView = null;
    break;
    }
    }
    return super.onInterceptTouchEvent(ev);
    } private void startDragging(Bitmap bm, int y) {
    stopDragging(); mWindowParams = new WindowManager.LayoutParams();
    mWindowParams.gravity = Gravity.TOP;
    mWindowParams.x = 0;
    mWindowParams.y = y - mDragPoint + mCoordOffset; mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    mWindowParams.format = PixelFormat.TRANSLUCENT;
    mWindowParams.windowAnimations = 0; ImageView v = new ImageView(getContext());
    v.setBackgroundColor(dragndropBackgroundColor);
    v.setImageBitmap(bm);
    mDragBitmap = bm; mWindowManager = (WindowManager) getContext()
    .getSystemService("window");
    mWindowManager.addView(v, mWindowParams);
    mDragView = v;
    } private void stopDragging() {
    if (mDragView != null) {
    WindowManager wm = (WindowManager) getContext().getSystemService(
    "window");
    wm.removeView(mDragView);
    mDragView.setImageDrawable(null);
    mDragView = null;
    }
    if (mDragBitmap != null) {
    mDragBitmap.recycle();
    mDragBitmap = null;
    } } private void dragView(int x, int y) {
    float alpha = 1.0f;
    mWindowParams.alpha = alpha;
    // }
    mWindowParams.y = y - mDragPoint + mCoordOffset;
    mWindowManager.updateViewLayout(mDragView, mWindowParams);
    } private int getItemForPosition(int y) {
    int adjustedy = y - mDragPoint -  mItemHeightHalf;
    int pos = myPointToPosition(0, adjustedy);
    if (pos >= 0) {
    if (pos <= mFirstDragPos) {
    pos += 1;
    }
    } else if (adjustedy < 0) {
    pos = 0;
    }
    return pos;
    } @SuppressWarnings("unused")
    private void adjustScrollBounds(int y) {
    if (y >= mHeight / 3) {
    mUpperBound = mHeight / 3;
    }
    if (y <= mHeight * 2 / 3) {
    mLowerBound = mHeight * 2 / 3;
    }
    } /*
     * Restore size and visibility for all listitems
     */
    private void unExpandViews(boolean deletion) {
    for (int i = 0;; i++) {
    View v = getChildAt(i);
    if (v == null) {
    if (deletion) {
    // HACK force update of mItemCount
    int position = getFirstVisiblePosition();
    int y = getChildAt(0).getTop();
    setAdapter(getAdapter());
    setSelectionFromTop(position, y);
    // end hack
    }
    layoutChildren(); // force children to be recreated where needed
    v = getChildAt(i);
    if (v == null) {
    break;
    }
    }
    ViewGroup.LayoutParams params = v.getLayoutParams();
    if (i != 3) {
    params.height = DensityUtil.dip2px(c, mItemHeightNormal);
    } else {
    params.height = DensityUtil.dip2px(c, 100);
    }
    v.setLayoutParams(params); v.setVisibility(View.VISIBLE);
    }
    } /*
     * Adjust visibility and size to make it appear as though an item is being
     * dragged around and other items are making room for it: If dropping the
     * item would result in it still being in the same place, then make the
     * dragged listitem's size normal, but make the item invisible. Otherwise,
     * if the dragged listitem is still on screen, make it as small as possible
     * and expand the item below the insert point. If the dragged item is not on
     * screen, only expand the item below the current insertpoint.
     */
    private void doExpansion() {
    int childnum = mDragPos - getFirstVisiblePosition();
    if (mDragPos > mFirstDragPos) {
    childnum++;
    } View first = getChildAt(mFirstDragPos - getFirstVisiblePosition()); for (int i = 0;; i++) {
    View vv = getChildAt(i);
    if (vv == null) {
    break;
    }
    int height = DensityUtil.dip2px(c, mItemHeightNormal);// mItemHeightNormal;
    int visibility = View.VISIBLE;
    if (vv.equals(first)) {
    // processing the item that is being dragged
    if (mDragPos == mFirstDragPos) {
    // hovering over the original location
    visibility = View.INVISIBLE;
    } else {
    // not hovering over it
    height = 1;
    }
    } else if (i == childnum) {
    if (mDragPos < getCount() - 1) {
    height = DensityUtil.dip2px(c, mItemHeightExpanded);
    }
    }
    ViewGroup.LayoutParams params = vv.getLayoutParams();
    params.height = height;
    vv.setLayoutParams(params);
    vv.setVisibility(visibility);
    }
    } /*
     * pointToPosition() doesn't consider invisible views, but we need to, so
     * implement a slightly different version.
     */
    private int myPointToPosition(int x, int y) {
    Rect frame = mTempRect;
    final int count = getChildCount();
    for (int i = count - 1; i >= 0; i--) {
    final View child = getChildAt(i);
    child.getHitRect(frame);
    if (frame.contains(x, y)) {
    return getFirstVisiblePosition() + i;
    }
    }
    return INVALID_POSITION;
    }

    public interface DropListener {
    void drop(int from, int to);
    } public void setDropListener(DropListener onDrop) {
    // TODO Auto-generated method stub
    mDropListener = onDrop;
    }}
      

  2.   


    04-09 15:40:58.692: E/AndroidRuntime(3362): java.lang.NullPointerException
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.eastcom.intelligentanswer.ui.SetScene$MyAdapter.getView(SetScene.java:200)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.widget.AbsListView.obtainView(AbsListView.java:1595)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.widget.ListView.fillDown(ListView.java:695)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.widget.ListView.fillSpecific(ListView.java:1327)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.widget.ListView.layoutChildren(ListView.java:1601)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.eastcom.intelligentanswer.adapter.ListViewInterceptor.unExpandViews(ListViewInterceptor.java:239)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.eastcom.intelligentanswer.adapter.ListViewInterceptor.onTouchEvent(ListViewInterceptor.java:77)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.View.dispatchTouchEvent(View.java:3938)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1746)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1152)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.app.Activity.dispatchTouchEvent(Activity.java:2102)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1730)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2218)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1889)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.os.Handler.dispatchMessage(Handler.java:99)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.os.Looper.loop(Looper.java:130)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at android.app.ActivityThread.main(ActivityThread.java:3691)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at java.lang.reflect.Method.invokeNative(Native Method)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at java.lang.reflect.Method.invoke(Method.java:507)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    04-09 15:40:58.692: E/AndroidRuntime(3362):  at dalvik.system.NativeStart.main(Native Method)