我已经为3个应用程序创建了两个带有共享代码和资源的android库项目。其中一个库(我称之为库A)具有由所有3个应用程序共享的代码和资源,另一个库中的代码和资源仅由三个中的两个共享(我们称之为库B)。从嵌套库继承的Activity中找不到资源
所以我让库B依赖A.我遇到的问题是依赖于库B的两个应用程序。当启动第一个Activity时,当尝试访问xml布局中的元素时,我得到NoSuchFieldErrors或NullPointerExceptions在库B中定义。它似乎无法从实例B中的Super类中找到资源。我创建了一个能够重现问题的小例子。
在程序库A:
AActivity.java:
public class AActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doStuff();
}
protected void doStuff() {
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlib"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testliba.AActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在库B:
BActivity.java:
public class BActivity extends AActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main_b);
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage(getResources().getString(R.string.buttonText));
}
@Override
protected void doStuff() {
Button b = (Button) findViewById(R.id.button1);
b.setText("I'm a button");
}
}
* RES /布局/ activity_main_b.xml:*
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="56dp"
android:text="@string/buttonText" />
</RelativeLayout>
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlib"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testlibb.BActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在应用这取决于图书馆Y B:
MainActivity.java:
public class MainActivity extends BActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Contents specific for this Activity
}
}
在根目录的应用程序的我发现三个R.java生成:
- com。示例。 testapp
- com.example.testlibb
- com.example.testliba
和id.button1存在既在R.java在testapp和testlibb并且它们具有在两个相同的值。仍然在运行时找不到。
在此先感谢
好抓!对不起,我在做这个例子时很sl and,并且认为NullPointerException是我在真实项目中遇到的同一问题的一个不同症状。 downvote是当之无愧的。当我根据您的意见修复我的示例项目时,我能够重现与我的实际项目中相同的问题。此后不久,我找到了解决方案(我已更新了我的问题,并会在遇到有人遇到同一问题时解决它)。谢谢您的帮助! – Simon