Note that there are some explanatory texts on larger screens.

plurals
  1. POResources not found in Activity inheriting from nested library
    primarykey
    data
    text
    <p>I've created two android library projects with shared code and resources for 3 apps. One of the libraries (I'll call it library A) has code and resources that are shared by all 3 apps and the other has code and resources that are shared only by two of the three (let's call it library B).</p> <p>So I made library B depend on A. The problem I'm having is with the two apps that depend on library B. When starting the first Activity I get NoSuchFieldErrors or NullPointerExceptions when trying to access the elements in a xml layout defined in library B. It seems that it can't find the resources from the Super class in labrary B. I've created a small example that reproduces the problem.</p> <p><strong>In library A:</strong></p> <p><em>AActivity.java:</em></p> <pre><code>public class AActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); doStuff(); } protected void doStuff() { } } </code></pre> <p><em>AndroidManifest.xml:</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testlib" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.testliba.AActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p><strong>In library B:</strong></p> <p><em>BActivity.java:</em></p> <pre><code>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"); } } </code></pre> <p>*res/layout/activity_main_b.xml:*</p> <pre><code>&lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><em>AndroidManifest.xml:</em></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testlib" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.testlibb.BActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p><strong>In app that depends on library B:</strong></p> <p><em>MainActivity.java:</em></p> <pre><code>public class MainActivity extends BActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Contents specific for this Activity } } </code></pre> <p>In the <em>gen</em> directory of the application I find three <em>R.java</em> generated:</p> <ul> <li>com.example.testapp</li> <li>com.example.testlibb</li> <li>com.example.testliba</li> </ul> <p>and <em>id.button1</em> is present both in the <em>R.java</em> in <em>testapp</em> and <em>testlibb</em> and they have the same value in both. Still at runtime it's not found.</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload