Luis A. Florit
Luis A. Florit

Reputation: 2599

Converting app to library: Intent gives NullPointerException

I made 2 apps: ave and xvf. From ave, I called an intent to open the class Thumbs inside xvf like this:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.floritfoto.apps.xvf", "com.floritfoto.apps.xvf.Thumbs");
intent.putExtra("files", imgfiles);
startActivity(intent);

With the 2 apps installed this worked like a charm, and the world was happy.

Now I made xvf a library to avoid having to install both apps. I am now calling Thumbs like this:

Intent intent = new Intent(getBaseContext(), com.floritfoto.apps.xvf.Thumbs.class);
intent.putExtra("files", imgfiles);
startActivity(intent);

Well, this is not working because I get a NullPointerException at a line in Thumbs that says:

gridview.setNumColumns(3);

gridview is a GridView that is created in the onCreate() method of Thumbs from its layout.

My sixth sense tells me that this problem is a context-related one. Maybe at getBaseContext() when calling the intent? What should I put there? (Sorry, this newbie doesn't understand this context thing much...)

I'm including the crash log:

12-22 13:54:20.890: W/dalvikvm(29093): threadid=1: thread exiting with uncaught exception (group=0x40c531f8)
12-22 13:54:20.895: E/AndroidRuntime(29093): FATAL EXCEPTION: main
12-22 13:54:20.895: E/AndroidRuntime(29093): java.lang.RuntimeException: Unable to start activity ComponentInfo    {com.floritfoto.apps.ave/com.floritfoto.apps.xvf.Thumbs}: java.lang.NullPointerException
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.os.Looper.loop(Looper.java:137)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.ActivityThread.main(ActivityThread.java:4507)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at java.lang.reflect.Method.invokeNative(Native Method)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at java.lang.reflect.Method.invoke(Method.java:511)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at dalvik.system.NativeStart.main(Native Method)
12-22 13:54:20.895: E/AndroidRuntime(29093): Caused by: java.lang.NullPointerException
12-22 13:54:20.895: E/AndroidRuntime(29093):    at com.floritfoto.apps.xvf.Thumbs.numcols(Thumbs.java:70)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at com.floritfoto.apps.xvf.Thumbs.onCreate(Thumbs.java:110)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.Activity.performCreate(Activity.java:4465)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-22 13:54:20.895: E/AndroidRuntime(29093):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
12-22 13:54:20.895: E/AndroidRuntime(29093):    ... 11 more

Upvotes: 0

Views: 206

Answers (1)

Luis A. Florit
Luis A. Florit

Reputation: 2599

Ok, this sounds crazy, but it worked. First, I deleted the functon numcols() and moved its code into the onCreate() method, right where the gridview is initialized. I did a project Clean, and now it works flawlessly.

EDIT

This happened several times when generating an .apk for the library, and all of them were corrected with the above method.

Upvotes: 1

Related Questions