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

Sim*_*mon 1 android android-library android-resources

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

所以我让库B依赖于A.我遇到的问题是依赖于库B的两个应用程序.当启动第一个Activity时,我尝试访问库B中定义的xml布局中的元素时得到NoSuchFieldErrors或NullPointerExceptions似乎它无法从工程师B中的超级类中找到资源.我创建了一个小例子来重现问题.

在图书馆A:

AActivity.java:

public class AActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        doStuff();
    }

    protected void doStuff() {
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在图书馆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");
}
}
Run Code Online (Sandbox Code Playgroud)

*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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在依赖于库B的应用程序中:

MainActivity.java:

public class MainActivity extends BActivity {

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

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

  • com.example.testapp
  • com.example.testlibb
  • com.example.testliba

并且id.button1存在于testapptestlibb中的R.java中,并且它们在两者中都具有相同的值.仍然在运行时它没有找到.

提前致谢

Sim*_*mon 5

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testlib"
Run Code Online (Sandbox Code Playgroud)

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

在图书馆A中

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testliba"
Run Code Online (Sandbox Code Playgroud)

在图书馆B

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testlibb"
Run Code Online (Sandbox Code Playgroud)