Reputation: 74
I have this problem with my ImageAdapter in Android Studio. I have huge performance problems when I start the application on a phone. So, people suggested, I should use a ViewHolder. I looked at dozens of examples on how others did it. tried lot's of versions. Still, my app crashes when I try to open that activity. Here's my code:
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
}; }
The program complies without any problem. But it crashes, when I open the activity. So I used debugger to see, when the app crashes. The app crashes, when
vi.setTag(holder);
should be performed. I appreciate every kind of help.
logcat:
--------- beginning of crash 07-21 13:42:19.229 3059-3059/com.b.a.test E/AndroidRuntime: FATAL EXCEPTION: main Process: com.b.a.test, PID: 3059 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setTag(java.lang.Object)' on a null object reference at com.b.a.test.ImageAdap.getView(ImageAdap.java:59) at android.widget.AbsListView.obtainView(AbsListView.java:2346) at android.widget.GridView.onMeasure(GridView.java:1065) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18788) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) at android.view.View.measure(View.java:18788) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 07-21 13:42:21.620 3059-3059/com.b.a.test I/Process: Sending signal. PID: 3059 SIG: 9 07-21 13:42:22.939 3315-3315/com.b.a.test W/System: ClassLoader referenced unknown path: /data/app/com.b.a.test-1/lib/x86 07-21 13:42:23.815 3315-3315/com.b.a.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 07-21 13:42:23.971 3315-3348/com.b.a.test D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 07-21 13:42:24.003 3315: 3315 D/ ] HostConnection::get() New Host Connection established 0xad1b97e0, tid
3315
Upvotes: 0
Views: 157
Reputation: 378
if(convertView == null)
You should inflate layout when that assertion is true. Because your view is null. Like that;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
If you don't want to inflate a layout you can use like that.
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
}
Upvotes: 1
Reputation: 73926
your code should look like this
if(convertView == null) {
holder = new ViewHolder();
ImageView imageView = new ImageView(mContext);
vi = imageView;
holder.image = imageView
vi.setTag(holder);
}
You need to set the view first
Upvotes: 1
Reputation: 9569
View vi = convertView;
if(convertView == null) {
vi.setTag(holder);
}
Just check your code carefully. Simply saying you invoke setTag
from null, and NullPointerException
throws because of that.
Upvotes: 0