我正在使用带有复选框、图像视图和 2 个文本框的列表视图。列出使用 araayaadapter 生成的内容,但我正在轻轻滚动列表视图,但当我以一定速度滚动列表视图时,它会崩溃。以下是我的代码。
导入java.io.File;导入 java.util.ArrayList;导入 java.util.List;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
@SuppressLint("SdCardPath")
public class AddOptionItemAdapter extends ArrayAdapter<String>
{
String MenuId="";
Context context;
String tag;
String tag1;
String ogFilePath="";
String imgpath="";
private List<NameBean> list;
ArrayList< String>priceList=new ArrayList<String>();
ArrayList< String>imagepathList=new ArrayList<String>();
public AddOptionItemAdapter(Context context, List<NameBean> list,ArrayList<String> priceList,ArrayList<String> imagePathList) {
super(context, R.layout.lvoptncat,priceList);
this.list=list;
this.priceList=priceList;
this.imagepathList=imagePathList;
this.context=context;
}
@TargetApi(16)
public View getView (int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.lvoptncat, null);
holder.tvOptnCatName=(com.example.hotelmenu.CustomTextView)convertView.findViewById(R.id.tvSubMenu);
holder.tvPrice=(com.example.hotelmenu.CustomTextView)convertView.findViewById(R.id.tvPrice);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.imgPath = (ImageView) convertView.findViewById(R.id.imgMenu);
holder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view,
boolean isChecked) {
int getPosition = (Integer) view.getTag();
addOptionActivity.optnCatNameList.get(getPosition).setSelected(view.isChecked());
}
});
}
else
holder = (ViewHolder) convertView.getTag();
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,"HotelMenuImages");
if(!file.exists())
{
file.mkdirs();
}
ogFilePath=file.getAbsolutePath();
//String str=getItem(position);
//String []OptnCatDetails=str.split(",");
//DataSource datasource=new DataSource(context);
try
{
//Log.d("ImagePath111",MenuDetails[3].trim());
//String photo2 =OptnCatDetails[2];
//Log.d("ImagePath1222",photo2);
String photo2=imagepathList.get(position);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = 4;
Bitmap myBitmap = BitmapFactory.decodeFile(ogFilePath+"/"+photo2, options);
holder.imgPath.setImageBitmap(myBitmap);
}
catch(OutOfMemoryError e)
{
Log.d("ImageError",""+e);
}
holder.tvOptnCatName.setText(list.get(position).getName());
holder.tvPrice.setText(priceList.get(position));
holder.checkbox.setTag(position);
holder.checkbox.setChecked(addOptionActivity.optnCatNameList.get(position).isSelected());
return convertView;
}
class ViewHolder {
protected com.example.hotelmenu.CustomTextView tvOptnCatName,tvPrice;
protected CheckBox checkbox;
protected ImageView imgPath;
}
}
以下是我的日志猫
07-19 10:56:13.549: E/InputEventReceiver(11356): Exception dispatching input event.
07-19 10:56:13.549: D/AndroidRuntime(11356): Shutting down VM
07-19 10:56:13.549: W/dalvikvm(11356): threadid=1: thread exiting with uncaught exception (group=0x4169a2a0)
07-19 10:56:13.565: E/AndroidRuntime(11356): FATAL EXCEPTION: main
07-19 10:56:13.565: E/AndroidRuntime(11356): java.lang.NullPointerException
07-19 10:56:13.565: E/AndroidRuntime(11356): at com.example.hotelmenu.AddOptionItemAdapter.getView(AddOptionItemAdapter.java:95)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.AbsListView.obtainView(AbsListView.java:2449)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.ListView.makeAndAddView(ListView.java:1769)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.ListView.fillUp(ListView.java:706)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.ListView.correctTooHigh(ListView.java:1395)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.ListView.fillGap(ListView.java:637)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5534)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3417)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3910)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.View.dispatchTouchEvent(View.java:7340)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2181)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1929)
07-19 10:56:13.565: E/AndroidRuntime(11356): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2113)
07-19 10:56:13.565: E/AndroidRuntime(11356): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1466)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
07-19 10:56:13.565: E/AndroidRuntime(11356): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.View.dispatchPointerEvent(View.java:7525)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3370)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3302)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4394)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4372)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4476)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4444)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4495)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.Choreographer.doFrame(Choreographer.java:523)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.os.Handler.handleCallback(Handler.java:615)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.os.Looper.loop(Looper.java:137)
07-19 10:56:13.565: E/AndroidRuntime(11356): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-19 10:56:13.565: E/AndroidRuntime(11356): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 10:56:13.565: E/AndroidRuntime(11356): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 10:56:13.565: E/AndroidRuntime(11356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-19 10:56:13.565: E/AndroidRuntime(11356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-19 10:56:13.565: E/AndroidRuntime(11356): at dalvik.system.NativeStart.main(Native Method)