-1

after searching for a solution for 3 hours without finding a solution I decided to fo for Ask Question. So I am using BaseAdapter to populate gridview with images and the error is on the line where I inflate the layout in getView() method of BaseAdapter

adapter.java

public class adapter extends BaseAdapter {
Context context;
int images[];
LayoutInflater inflater;
public adapter(final Context applicationcontext, final int[] images)
{
    this.context=applicationcontext;
    this.images=images;
    inflater=(LayoutInflater.from(applicationcontext));
}

@Override
public int getCount() {
    return images.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    try{
        view =inflater.inflate(R.layout.images_list, null);// error line
        ImageView imageView=(ImageView) view.findViewById(R.id.icon);
        imageView.setImageResource(images[position]);

    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    return view;
}

}

here is my xml

images_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dp"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher_foreground" />

</LinearLayout>

stacktrace:

   01-15 18:38:37.729 19417-19417/com.example.zain.testingwallpaper E/AndroidRuntime: FATAL EXCEPTION: main
   java.lang.NullPointerException
       at android.widget.AbsListView.obtainView(AbsListView.java:2474)
       at android.widget.GridView.onMeasure(GridView.java:1030)
       at android.view.View.measure(View.java:15524)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1986)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1227)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
       at android.view.Choreographer.doCallbacks(Choreographer.java:555)
       at android.view.Choreographer.doFrame(Choreographer.java:525)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
       at android.os.Handler.handleCallback(Handler.java:615)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4921)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
       at dalvik.system.NativeStart.main(Native Method)
   01-15 18:38:37.819 19417-19417/com.example.zain.testingwallpaper I/Process: Sending signal. PID: 19417 SIG: 9
Selvin
  • 6,441
  • 3
  • 35
  • 42

1 Answers1

-2

Replace below line

view =inflater.inflate(R.layout.images_list, null);

with

view =inflater.inflate(R.layout.images_list, parent, false);
Poras Bhardwaj
  • 843
  • 1
  • 12
  • 33
  • what error this would fix and how? there is method `LayoutInflater.inflate(int, android.view.ViewGroup)` ... while using `inflate(int resource, ViewGroup root, boolean attachToRoot)` is better that will not fix "error" (unless he has only warning - but in the questrion is **error**) – Selvin Jan 15 '18 at 12:20
  • @Selvin The parent is provided so you can inflate your view into that for proper layout parameters. The parent parameter of the getView() method gives a reference to the parent layout which has the listView – Poras Bhardwaj Jan 15 '18 at 12:23
  • I know what it does but how this will fix **an error** which he is asked about ... – Selvin Jan 15 '18 at 12:24
  • He is passing null instead of parent view while inflating the view. He should pass rootView/parent – Poras Bhardwaj Jan 15 '18 at 12:27
  • *He is passing null instead of parent view while inflating the view. He should pass rootView/parent* **This not causing an error ... it is only bad for performance!** ... again, yes, it is good to use `inflate(int resource, ViewGroup root, boolean attachToRoot)` but **it will not fix his error** – Selvin Jan 15 '18 at 12:28
  • replacing the code did not work as you suggested – Zain Arshad Jan 15 '18 at 12:32
  • @Selvin So, please let us know the cause of error & help Zain Arshad out. – Poras Bhardwaj Jan 15 '18 at 12:34