1

3つのアプリの共有コードとリソースを使用して2つのAndroidライブラリプロジェクトを作成しました。ライブラリの1つ(ライブラリAと呼びます)には、3つのアプリすべてで共有されるコードとリソースがあり、もう1つには、3つのうち2つだけで共有されるコードとリソースがあります(ライブラリBと呼びます)。

そこで、ライブラリBをAに依存させました。私が抱えている問題は、ライブラリBに依存する2つのアプリにあります。最初のアクティビティを開始すると、ライブラリ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() {
    }

}

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>

In library 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>

In app that depends on library B:

MainActivity.java:

public class MainActivity extends BActivity {

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

In the gen directory of the application I find three R.java generated:

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

and id.button1 is present both in the R.java in testapp and testlibb and they have the same value in both. Still at runtime it's not found.

Thanks in advance

4

2 に答える 2

5

The problem turned out to be that the AndroidManifest.xml in both library projects had the same default package

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

this caused problems when generating R.java. So to fix the issue I just had to change the default packages of the library projects so that they differed from eachother:

In library A

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

And in library B

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testlibb"
于 2013-01-09T11:57:48.493 に答える
1

In BActivity you are calling super.onCreate(savedInstanceState); After this following in AActivity is called

super.onCreate(savedInstanceState);
doStuff();

doStuff(); is overrided by BActivity

so Button b = (Button) findViewById(R.id.button1); is called, but setContentView(R.layout.activity_main_b); still has not been called at this point

于 2012-12-29T15:32:13.640 に答える