gpt4 book ai didi

android - 在从嵌套库继承的 Activity 中找不到资源

转载 作者:行者123 更新时间:2023-11-29 14:52:29 25 4
gpt4 key购买 nike

我已经为 3 个应用程序创建了两个具有共享代码和资源的 android 库项目。其中一个库(我将其称为库 A)具有由所有 3 个应用程序共享的代码和资源,另一个具有仅由三个应用程序中的两个共享的代码和资源(我们将其称为库 B)。

所以我让库 B 依赖于 A。我遇到的问题是依赖于库 B 的两个应用程序。当启动第一个 Activity 时,我在尝试访问定义的 xml 布局中的元素时得到 NoSuchFieldErrors 或 NullPointerExceptions在图书馆 B 中。它似乎无法从图书馆 B 中的父类(super class)中找到资源。我创建了一个重现该问题的小示例。

在库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/layout/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>

在依赖库 B 的应用中:

MainActivity.java:

public class MainActivity extends BActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Contents specific for this Activity
}
}

在应用程序的 gen 目录中,我发现生成了三个 R.java:

  • com.example.testapp
  • com.example.teSTLibb
  • com.example.teSTLiba

id.button1 都存在于 testappteSTLibbR.java 中,并且它们有两者的值(value)相同。仍然在运行时找不到它。

提前致谢

最佳答案

问题原来是两个库项目中的AndroidManifest.xml有相同的默认包

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlib"

这会导致生成 R.java 时出现问题。因此,为了解决这个问题,我只需要更改库项目的默认包,使它们彼此不同:

在图书馆A

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testliba"

在图书馆B

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testlibb"

关于android - 在从嵌套库继承的 Activity 中找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14082881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com