{"id":1884,"date":"2022-08-30T15:20:15","date_gmt":"2022-08-30T15:20:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/null-pointer-exception-in-viewpager-fragment-with-gridview-and-custom-adapter-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:15","modified_gmt":"2022-08-30T15:20:15","slug":"null-pointer-exception-in-viewpager-fragment-with-gridview-and-custom-adapter-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/null-pointer-exception-in-viewpager-fragment-with-gridview-and-custom-adapter-collection-of-common-programming-errors\/","title":{"rendered":"Null Pointer exception in ViewPager Fragment with GridView and custom Adapter-Collection of common programming errors"},"content":{"rendered":"<p>I am getting a null pointer exception on the line where I am setting the adapter (at mGrid.setAdapter(new AppsAdapter());. As far as I can see everything in the adapter is pointing to something valid. I am trying to display a grid of icons that are the App icons found in the app drawer (and the app list in Settings).<\/p>\n<p>Here is the code for the Fragment:<\/p>\n<pre><code>public class Tab3_fragment extends Fragment {\n\nGridView mGrid;\nprivate List mApps = new ArrayList();\n\n\npublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){\n    return inflater.inflate(R.layout.tabfragment3, container, false);\n\n}\n\n@Override\npublic void onStart() {\n    super.onStart();\n\n    loadApps();\n    mGrid = (GridView) getActivity().findViewById(R.id.myGrid);\n    mGrid.setAdapter(new AppsAdapter());\n\n}\n\n@Override\npublic void onResume() {\n    super.onResume();\n    loadApps();\n}\n\nprivate void loadApps() {\n    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);\n    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);\n\n    mApps = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);\n}\n\npublic class AppsAdapter extends BaseAdapter {\n    public AppsAdapter() {\n    }\n\n    public View getView(int position, View convertView, ViewGroup parent) {\n        ImageView i;\n\n        if (convertView == null) {\n            i = new ImageView(getActivity().getApplicationContext());\n            i.setScaleType(ImageView.ScaleType.FIT_CENTER);\n            i.setLayoutParams(new GridView.LayoutParams(50, 50));\n        } else {\n            i = (ImageView) convertView;\n        }\n\n        ResolveInfo info = mApps.get(position);\n        i.setImageDrawable(info.activityInfo.loadIcon(getActivity().getPackageManager()));\n\n        return i;\n    }\n\n\n    public final int getCount() {\n        return mApps.size();\n    }\n\n    public final Object getItem(int position) {\n        return mApps.get(position);\n    }\n\n    public final long getItemId(int position) {\n        return position;\n    }\n  } \n}\n<\/code><\/pre>\n<p>Here is the code for the Layout (which is included the &#8220;tab3fragment.xml&#8221; inside a scrollView):<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p>When I swipe over to the second tab (of 3) it suddenly crashes and gives me these errors:<\/p>\n<pre><code>04-27 21:31:51.337: D\/libEGL(16874): loaded \/vendor\/lib\/egl\/libEGL_POWERVR_SGX540_120.so\n04-27 21:31:51.369: D\/libEGL(16874): loaded \/vendor\/lib\/egl\/libGLESv1_CM_POWERVR_SGX540_120.so\n04-27 21:31:51.369: D\/libEGL(16874): loaded \/vendor\/lib\/egl\/libGLESv2_POWERVR_SGX540_120.so\n04-27 21:31:51.627: D\/OpenGLRenderer(16874): Enabling debug mode 0\n04-27 21:31:51.634: I\/Choreographer(16874): Skipped 110 frames!  The application may be doing too much work on its main thread.\n04-27 21:32:22.994: D\/AndroidRuntime(16874): Shutting down VM\n04-27 21:32:22.994: W\/dalvikvm(16874): threadid=1: thread exiting with uncaught exception (group=0x41c92930)\n04-27 21:32:23.009: E\/AndroidRuntime(16874): FATAL EXCEPTION: main\n04-27 21:32:23.009: E\/AndroidRuntime(16874): java.lang.NullPointerException\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at com.rdx.themetesterplus.Tab3_fragment.onStart(Tab3_fragment.java:36)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.Fragment.performStart(Fragment.java:1484)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1112)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.view.Choreographer.doFrame(Choreographer.java:531)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.os.Handler.handleCallback(Handler.java:725)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.os.Handler.dispatchMessage(Handler.java:92)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.os.Looper.loop(Looper.java:137)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at android.app.ActivityThread.main(ActivityThread.java:5226)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at java.lang.reflect.Method.invokeNative(Native Method)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at      java.lang.reflect.Method.invoke(Method.java:511)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)\n04-27 21:32:23.009: E\/AndroidRuntime(16874):    at dalvik.system.NativeStart.main(Native Method)\n<\/code><\/pre>\n<p>Any help would be appreciated. Thanks in advance \ud83d\ude42<\/p>\n<ol>\n<li>\n<p>Just an observation, not sure if its the issue or not, but from my understanding onStart() and onResume() happen right after each other in the start of the life cycle of a Fragment 1. I noticed loadApps() happens in each. From what I can tell, the loadApps() function is called twice on start up. Correct me if Im wrong, just thought I would mention it.<\/p>\n<p>Im not sure if you can reference the mGrid until the View has been setup first. Usually, once the view has been set, then you can use the findViewByID() method.<\/p>\n<p>Source Fragment Life Cycle Source<\/p>\n<\/li>\n<li>\n<p>Try putting<\/p>\n<pre><code>loadApps();\nmGrid = (GridView) getActivity().findViewById(R.id.myGrid);\nmGrid.setAdapter(new AppsAdapter());\n<\/code><\/pre>\n<p>your data setup code in onActivityCreated() method instead of onStart().This should resolve your problem. Let me know if you are still getting same prob.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:11:12. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am getting a null pointer exception on the line where I am setting the adapter (at mGrid.setAdapter(new AppsAdapter());. As far as I can see everything in the adapter is pointing to something valid. I am trying to display a grid of icons that are the App icons found in the app drawer (and the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1884","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1884"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1884\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}