David Doria
David Doria

Reputation: 10273

findViewById returning null inside a jar

I have a small piece of code that works correctly in its own project (it is just a file selection dialog). I wanted to use it in my larger project, so I exported the FileSelector as a jar by doing the following:

I was getting the error "Error generating final archive: Found duplicate file for APK: AndroidManifest.xml", so I just unselected AndroidManifest.xml from the list of files to export to the jar and created it again.

MyProject seems to build fine, but when I run it and click the button that should display the file selection dialog, I get a null pointer exception. I went back and tracked it down in the FileSelection code, and it is a simple

mFilterSpinner = (Spinner) mDialog.findViewById(R.id.fileFilter);

that is returning null. Is there something I have not done in the project setup that will allow a call like this to work from inside a jar? Or is there something wrong with my procedure for packaging up and importing the jar?

Upvotes: 1

Views: 467

Answers (2)

kingori
kingori

Reputation: 2616

Values in R class would be modified. So, you can't find your view when using compiled R inside your Jar.

The only solution is using android library project and reference resources and sources through it. Otherwise, you can referencing it by making your android library project into AAR archive.

Upvotes: 1

Nitin Sethi
Nitin Sethi

Reputation: 1336

if you are using Android UI components, I think you should be using it in another project as an Android library.

Upvotes: 0

Related Questions