简体   繁体   English

JAR文件中的startActivity时发生NullPointerException

[英]NullPointerException when startActivity from JAR file

I have a child project that must be included in parent project. 我有一个子项目,必须包含在父项目中。 What I did: 我做了什么:

  1. Mark child as library and compile 将child标记为库并进行编译
  2. Took resulted .jar and put it in \\libs folder from a parent project. 拿到了.jar文件,并将其放在父项目的\\ libs文件夹中。
  3. Copied all res used in child project in parent \\res folder. 将子项目中使用的所有res复制到父\\ res文件夹中。
  4. Add .JAR file in Properties->Java Build Path->libraries. 在Properties-> Java Build Path-> libraries中添加.JAR文件。
  5. Call StartActivity from parent project to an activity from child project. 从父项目调用StartActivity到子项目调用一个活动。

After running parent project it crashes. 运行父项目后,它崩溃。 Callstack says that is NullPointerException on: 调用堆栈说这是NullPointerException在:

final LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
layout .setOnClickListener(this);

This means that layout is null. 这意味着布局为空。

I should mention that running a child project as a single project works perfectly. 我应该提到将子项目作为单个项目运行是完美的。

What did I do wrong? 我做错了什么?

You can not use a .jar file if you "child" project depends on resources files. 如果“子”项目取决于资源文件,则不能使用.jar文件。 You need to import your project as a library (with source code) instead. 您需要将您的项目导入为一个库(带有源代码)。

  1. Copy your child project (all source code + resources) into your parent project libs/your_child_project_name/ directory 将您的子项目(所有源代码和资源)复制到父项目的libs / your_child_project_name /目录中
  2. Setup your build system (Gradle or Ant) to use the third party library 设置您的构建系统(Gradle或Ant)以使用第三方库
  3. Check for dependencies conflicts (ie both child and parent use the android support .Jar file, in that case remove the one in the child project and use the one in the parent project) 检查是否存在依赖项冲突(例如,子级和父级都使用android支持.Jar文件,在这种情况下,请删除子级项目中的一个,并在父级项目中使用一个)
  4. Rebuild your parent project 重建父项目
  5. Now you can import from the child project 现在您可以从子项目中导入

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

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