I have an Android project that can work on its own. I want to create another Android project that extends the previous project. But it throws a NoSuchFieldException when the library project tries to create some kind of component that uses findViewById (R.id.something).
Here is the code for the library project:
package com.example.android.effectivenavigation; import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; public class MainActivity extends FragmentActivity implements ActionBar.TabListener { protected AppSectionsPagerAdapter mAppSectionsPagerAdapter; protected ViewPager mViewPager; public void onCreate(Bundle savedInstanceState) {
And a project expanding it:
import com.example.android.effectivenavigation.MainActivity; import com.example.android.effectivenavigation.R; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.Menu; public class MainActivity2 extends MainActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
and here comes NoSuchFieldException:
mViewPager = (ViewPager) findViewById(R.id.pager); java.lang.NoSuchFieldError: com.example.android.effectivenavigation.R$id.pager
I believe the same thing happens when creating components using findViewById () in the library project. Any solution? I want the childβs project to be as easy as possible by delegating all projects to the library project.
So the library project announces the ViewPager:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
Quote from developer.android.com: Thrown when VM notices that a program is trying to reference a class or object, a field that does not exist . But ViewPager is declared and works fine on its own, not as a library project.
source share