简体   繁体   English

findViewById在罐子内返回null

[英]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: 我想在较大的项目中使用它,因此我通过执行以下操作将FileSelector导出为jar:

  • Right click on the FileSelector project and click 'Export' 右键单击FileSelector项目,然后单击“导出”

  • Select Java -> JAR file 选择Java-> JAR文件

  • Set the 'export destination' 设置“导出目的地”

  • Click 'Finish' 点击“完成”

  • Copy the exported .jar file into MyProject/libs 将导出的.jar文件复制到MyProject / libs中

  • Right click on MyProject -> Refresh 右键单击MyProject->刷新

  • Right click on MyProject -> Properties 右键单击MyProject->属性

  • Click 'Java Build Path' 点击“ Java构建路径”

  • In the 'Libraries' tab, click "Add JARs" and select the .jar file I just copied into MyProject 在“库”选项卡中,单击“添加JAR”,然后选择我刚刚复制到MyProject中的.jar文件。

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. 我收到错误消息“生成最终归档文件时出错:找到APK的重复文件:AndroidManifest.xml”,因此我只是从文件列表中取消选择了AndroidManifest.xml,然后将其导出到jar中并再次创建它。

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. MyProject似乎构建良好,但是当我运行它并单击应该显示文件选择对话框的按钮时,出现空指针异常。 I went back and tracked it down in the FileSelection code, and it is a simple 我回过头来在FileSelection代码中找到了它,这很简单

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

that is returning null. 返回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? 我在项目设置中没有做过哪些事情可以让这样的调用从jar中进行? Or is there something wrong with my procedure for packaging up and importing the jar? 还是包装和导入罐子的程序有问题?

Values in R class would be modified. R类中的值将被修改。 So, you can't find your view when using compiled R inside your Jar. 因此,在Jar中使用编译的R时找不到视图。

The only solution is using android library project and reference resources and sources through it. 唯一的解决方案是使用android库项目并通过它引用资源和资源。 Otherwise, you can referencing it by making your android library project into AAR archive. 否则,您可以通过将android库项目放入AAR存档中来引用它。

如果您使用的是Android UI组件,我认为您应该在另一个项目中将其用作Android库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM