jpardogo / PagerSlidingTabStrip

An interactive indicator to navigate between the different pages of a ViewPager
2.19k stars 353 forks source link

java.lang.NoSuchFieldError: com.astuetz.pagerslidingtabstrip.R$id.tab_title #59

Closed younes0 closed 9 years ago

younes0 commented 9 years ago

could you please explain to me this error? This doesn't happen when I add an textView with tab_title id, docs don't mention to add it although, and still I get other issue, the view is blank...

java.lang.NoSuchFieldError: com.astuetz.pagerslidingtabstrip.R$id.tab_title
            at com.astuetz.PagerSlidingTabStrip.addTab(PagerSlidingTabStrip.java:279)
            at com.astuetz.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:253)
            at com.astuetz.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:236)
jpardogo commented 9 years ago

What is your layout file? I think that this is what is happening

younes0 commented 9 years ago

Thanks for your response

this is my tabs fragment layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
            android:id="@+id/tab_title"/>
    <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

    <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@android:color/white"/>
</LinearLayout>

And this is the tab fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:choiceMode="multipleChoice"
        android:dividerHeight="1dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
jpardogo commented 9 years ago

Why is this TextView over the PagerSlidingTabStrip view?

younes0 commented 9 years ago

Sorry that was for testing purposes. When I'm adding that TextView, it doesn't produce the mentionned error.

This is what I get when I put the TextView below untitled Tab / Fragment switching works but tab title isn't shown as you can see you (no problem related to the adapter as it returns title)

jpardogo commented 9 years ago

Are you trying to use a custom tab? What are you actually trying to do?

younes0 commented 9 years ago

I don't have any custom tab, it is required?

jpardogo commented 9 years ago

No, but I thought it was the feature you were using. Can you explain into detail what you are doing? It seems to be a problem in your code. Paste the adapter.

younes0 commented 9 years ago
public class FilterFragment extends Fragment {

    protected ViewPager pager;
    protected MaterialTabHost tabHost;
    protected MyPagerAdapter adapter;

    @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_filter_tabs, container, false);
        ButterKnife.inject(this, view);

        // Initialize the ViewPager and set an adapter
        ViewPager pager = (ViewPager) view.findViewById(R.id.viewpager);
        pager.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));

        // Bind the tabs to the ViewPager
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        tabs.setViewPager(pager);

        return view;
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "hello";
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
             return new FilterOriginsFragment();
        }
    }
}

public class FilterOriginsFragment extends Fragment {

    public FilterOriginsFragment() {}

    protected View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_filter_origins, container, false);
        setHasOptionsMenu(true);
/* .. */
        return mView;
    }
}
jpardogo commented 9 years ago

You have 2 fragments but just 1 Title?

younes0 commented 9 years ago

@jpardogo this is for debugging purposes

jpardogo commented 9 years ago

You are not getting hello hello? when does it crash?

jpardogo commented 9 years ago

Are you using gradle? Or you added the library yourself as a library project?

younes0 commented 9 years ago

It crashes as I instanciate the fragment with a custom navigation drawer: https://github.com/neokree/MaterialNavigationDrawer

The image you saw is when I add

  <TextView android:layout_width="match_parent" android:layout_height="match_parent"
            android:id="@+id/tab_title"/>

Which I don't want since I'm fine with the default tab layout.. When I don't add that, it crashes

I'm using gradle

jpardogo commented 9 years ago

You are using a ViewPager with fragments inside a fragment:

1.I would not complicate that much the app architecture, unless it is strictly necessary.

  1. If you still want to follow this path , you should pass getChildFragmentManager() instead of getActivity().getSupportFragmentManager()

I am not sure that it is the problem but at least , worth to mention. Give it a try and let me know

younes0 commented 9 years ago
  1. problem is, I'm using a custom navigation drawer which works with fragments
  2. unfortunately, that didn't work, getting the same error & crash.

Thanks for your help...

jpardogo commented 9 years ago

Do you have any layout in your project called tab.xml?

jpardogo commented 9 years ago

http://stackoverflow.com/questions/16204667/nosuchfielderror-on-findviewbyid

"If you have one project working as a library that declares some id in a given XML file, then your main project redefines that XML, the original id is gone, but you won't see the error until run time."

"The solution may be: The name of xml file is repeated, eg:a.xml is in your library project, at the same time, a.xml is in your main project. so, please change one of the xml name."

Did you redefined tab.xml? tab.xml

younes0 commented 9 years ago

I have checked that SO question: I don't have any tab.xml or any duplicate xml filename file or any ressource called tab_title

younes0 commented 9 years ago

wait... now I'm doubtful, I still have that other similar library in gradle. Keep you informed in 5 min

jpardogo commented 9 years ago

I have actually implemented the ViewPager inside a fragment last week for the same reason you are doing it. And It worked fine... I am really curious about this error.... Don't you see any difference between the example code and yours? Did you try to create a fresh new project and implement a simplify example to see what could be happening and find the error?

jpardogo commented 9 years ago

If you have more libraries about tabs it could be that 2 of the resources are being overwriten. Two tab.xml layouts.

jpardogo commented 9 years ago

That's the problem:

https://github.com/neokree/MaterialTabs/blob/master/MaterialTabsModule/src/main/res/layout/tab.xml

younes0 commented 9 years ago

You're right: I still had 'it.neokree:MaterialTabs:0.11' as dependencie which uses the same XML ids ! May I suggest you to namespace the XML ids

younes0 commented 9 years ago

Thank you so much for your time