Android Studio中的ImageAdapter存在此问题。当我在手机上启动应用程序时,我遇到了巨大的性能问题。所以,人们建议,我应该使用ViewHolder。我查看了其他人如何做到的几十个例子。尝试了许多版本。不过,当我尝试打开该活动时,我的应用程序崩溃了。这是我的代码:
public class ImageAdap extends BaseAdapter {
private Context mContext;
public ImageAdap(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null; //mThumbIds[position];
}
public long getItemId(int position) {
return position;
}
static class ViewHolder {
ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
holder.image = new ImageView(mContext);
vi.setTag(holder); //Here is the point the app crashes
}
else {
holder = (ViewHolder) vi.getTag();
}
holder.image.setLayoutParams(new GridView.LayoutParams(800, 600));
holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.image.setPadding(8, 8, 8, 8);
holder.image.setImageResource(mThumbIds[position]);
return vi;
}
// references to my images
private Integer[] mThumbIds = {
R.drawable.sampleda_01,
R.drawable.sampleda_02,
R.drawable.sampleda_03,
R.drawable.sampleda_04,
R.drawable.sampleda_05
}; }
该程序没有任何问题。但是当我打开活动时它会崩溃。当应用程序崩溃时,我使用调试器来查看。
时,应用程序崩溃了vi.setTag(holder);
应该执行。我感谢各种帮助。
logcat的:
---------崩溃开始07-21 13:42:19.229 3059-3059 / com.b.a.test E / AndroidRuntime:FATAL 例外:主要 过程:com.b.a.test,PID:3059 java.lang.NullPointerException:尝试调用虚方法' void android.view.View.setTag(java.lang.Object中)'在null对象引用上 在 com.b.a.test.ImageAdap.getView(ImageAdap.java:59) 在android.widget.AbsListView.obtainView(AbsListView.java:2346) 在android.widget.GridView.onMeasure(GridView.java:1065) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在 android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18788) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在 com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643) 在android.view.View.measure(View.java:18788) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)07-21 13:42:21.620 3059-3059 / com.b.a.test I / Process:发送 信号。 PID:3059 SIG:9 07-21 13:42:22.939 3315-3315 / com.b.a.test W / System:引用的ClassLoader 未知路径:/data/app/com.b.a.test-1/lib/x86 07-21 13:42:23.815 3315-3315 / com.b.a.test W / art:之前 Android 4.1,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode) 会错误地覆盖package-private方法 android.graphics.drawable.Drawable 07-21 13:42:23.971 3315-3348 / com.b.a.test D / OpenGLRenderer:使用 EGL_SWAP_BEHAVIOR_PRESERVED:true
[ 07-21 13:42:24.003 3315: 3315 D/ ] HostConnection::get() New Host Connection established 0xad1b97e0, tid
3315
答案 0 :(得分:1)
if(convertView == null)
当断言为真时,您应该扩充布局。因为您的视图为空。像那样;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
如果您不想给布局充气,可以这样使用。
View vi = convertView;
ViewHolder holder;
if(convertView == null) {
vi = new ImageView(mContext);
holder = new ViewHolder();
holder.image =(ImageView) vi;
vi.setTag(holder); //Here is the point the app crashes
}
答案 1 :(得分:1)
您的代码应如下所示
if(convertView == null) {
holder = new ViewHolder();
ImageView imageView = new ImageView(mContext);
vi = imageView;
holder.image = imageView
vi.setTag(holder);
}
您需要先设置视图
答案 2 :(得分:0)
View vi = convertView;
if(convertView == null) {
vi.setTag(holder);
}
请仔细检查您的代码。简单地说你从null调用setTag
,并NullPointerException
因此而抛出。