TabLayout crashing after updating support library to 23.2.0 - android

I am using TabLayout from the design library with the ViewPager, linking them using the function setupWithViewPager. The app is crashing in scenarios where it recreates the tabs, after the tab contents have been changed. Crash trace :
java.lang.IllegalArgumentException: Tab belongs to a different TabLayout.
at android.support.design.widget.TabLayout.addTab(TabLayout.java:433)
at android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:772)
at android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:763)
at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:715)
The crash is occuring after updating to support library 23.2.0, doesn't reproduce till v23.1.1.

Just found that this is an internal bug in Support library v23.2.0, registered at : https://code.google.com/p/android/issues/detail?id=201827

This was bug reported on google https://code.google.com/p/android/issues/detail?id=201827
But after release of Android Support Library, revision 23.2.1 (March 2016) This is fixed now.
just update Support Library to Android Support Library to 23.2.1

I have meet the same problem, and then I found the newer TabLayout use a pool to cache Tab.
in 23.1.1
public Tab newTab() {
return new Tab(this);
}
and in 23.2.0
public Tab newTab() {
Tab tab = sTabPool.acquire();
if (tab == null) {
tab = new Tab(this);
}
tab.mView = createTabView(tab);
return tab;
}
so if you use newTab() to create a Tab, and for some reason you didn't add it to the TableLayout. the next time you enter another activity with a TabLayout, this would happen.

I can still see this issue in support lib version: 25.3.1. So to avoid the crash, removedAllTabs() and created a new instance for the tab again and added to Tablayout.
gauge_tab.removeAllTabs()
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_04))
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_06))
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_08))

Related

Xamarin Forms FlyoutPage on landscape tablet

I am running the latest Xamarin Forms on VS 2019 community edition.
I just started the basic Xamarin Forms project and added a Flyout page template to it.
When running this on the Android emulator it works fine, but the menu won't close after selecting a page.
The code has not changed apart from adding another page to the menu items.
The code isPresented = false is still there but has no effect whatsover.
Is it simply not possible to have the menu collapse in landscape mode? Then what are my alternatives?
You just have to add in the MenuPage (the main file, the first one that is generated) the following property:
FlyoutLayoutBehavior = "Popover"
public MasterMenuItem ()
{
InitializeComponent ();
MasterBehavior = MasterBehavior.Popover; // This solve my problem
}

Using actionbarsherlock with android-support-v4 (Version 23)

I'm developing an app with actionbarsherlock and the ABS project is currently using android-support-v4 library (Version 18). Now I want to extend my project to support Android 6.0 and in order to use some of the methods like
ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR)
or
ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)
I require support library version 23. But ABS project is not compatible with this latest library and gives error like mAdded cannot be resolved or is not a field in Watson.java
Also, please don't suggest me to use AppCompatActivity instead of ABS as I tried it but getting stuck in a web of other mess as my project is quite big.
I'm facing the same problem. Here's my solution:
Clone ActionBarSherlock
No instance field mFragments of type Landroid/support/v4/app/FragmentManagerImpl;
// android.support.v4.app.FragmentActivity
// com.android.support:support-v4:22.+
final FragmentManagerImpl mFragments = new FragmentManagerImpl();
// com.android.support:support-v4:23.+
final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
// android.support.v4.app.FragmentManager.FragmentManagerImpl
ArrayList<Fragment> mAdded;
So we need to get instance of FragmentManagerImpl to access mAdded field
// android.support.v4.app.FragmentActivity
public FragmentManager getSupportFragmentManager() {
return mFragments.getSupportFragmentManager();
}
// android.support.v4.app.FragmentController
public FragmentManager getSupportFragmentManager() {
return mHost.getFragmentManagerImpl();
}
Add the following method to the android.support.v4.app.Watson class
#Nullable
private List<Fragment> getAddedFragments() {
return ((FragmentManagerImpl) getSupportFragmentManager()).mAdded;
}
Add the following code to onCreatePanelMenu, onPreparePanel and onMenuItemSelected methods and replace mFragments.mAdded with fragments
List<Fragment> fragments = getAddedFragments();
FloatMath
Historically these methods were faster than the equivalent double-based
{java.lang.Math} methods. On versions of Android with a JIT they
became slower and have since been re-implemented to wrap calls to
{java.lang.Math}. {java.lang.Math} should be used in
preference.
All methods were removed from the public API in version 23.
#deprecated Use {java.lang.Math} instead.
Replace all of occurrences of FloatMath with Math in com.actionbarsherlock.internal.nineoldandroids.view.animation.AnimatorProxy

Getting the v7 compat ActionBarContainer

As part of an app I'm working on, I'm attempting to obtain a reference to the ActionBar's container view. I used the answer from this question, and it's working nicely for me as long as the Android device in question is running API level 11 or up. However, the app needs to work as far back as API level 9 (the target API level is 19), and Gingerbread devices are giving me problems. I was originally using Sherlock for the project, but recently made the switch over to the v7 compat library instead. I can see and interact with the action bar in normal ways on Gingerbread devices (buttons work, etc.), but it fails when I attempt to get the container. The code I'm using is this (note - it's running inside a subclass of ActionBarActivity):
private FrameLayout getActionBarContainer() {
FrameLayout result = null;
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
try {
result = (FrameLayout)getWindow().getDecorView().findViewById(resId);
}
catch (Exception e) {
// If we get an exception, just eat it
}
return result;
}
To answer a few questions before they get asked:
resId resolves to a proper ID value on v11 devices and up, but resolves to 0 on pre-v11 devices.
The code that uses this has proper checks to handle a null result, which is why I'm just eating the exceptions. The try/catch block is mostly just there in case, by some freak occurrence, a ClassCastException manages to get thrown (which it never should, since the container is a subclass of FrameLayout).
I've checked and re-checked my imports; all of my ActionBar references (and all things related, like the ActionBarActivity superclass I'm extending) are the v7 compat library versions, not the standard library versions.
The action_bar_container ID should exist within the v7 compat library, if this is any indication.
I'm about out of ideas at this point. Is there something simple I'm missing? Any suggestions will be appreciated, and if you need more context/clarification, let me know.
I think your error is on the last parameter of
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
For api level <11 the package should be your application's package and not the plataform's package "android"

SlidingMenu and ViewPager with API13 (Honeycomb)

I´m experiencing a weird problem with SlidingMenu Library and ViewPager when they are running on devices with Android 3.2 (Honeycomb).
The problem appears when we "toggle" the SlidingMenu to show the Menu that is hidden on the left of the app. When we do this, both ContentView and BehingContentView stops responding to touch events.
Thinking that this was a problem related to my application, I downloaded the last version of ABS and SlidingMenu library and configured a new project using the built-in example that comes with the SlidingMenu and, for my surprise, the same behavior occurred with the ViewPager example.
These are the steps that I did:
Configure an Emulator using API Level 13 and 7" WSVGA (Tablet);
Download ABS and SlidingMenu from GIT;
Setup a new Project, using the compatibility library android-support-v41 (Also tested with android-support-v4);
Solved the problem 'getSupportActionBar() is undefined' as described here: https://github.com/jfeinstein10/SlidingMenu/issues/145;
Run the 'Example Application' and choose 'ViewPager' example;
Swipe pages to the right and to the left, without opening the menu;
Open the menu. See that the lists don´t scroll as expected;
Close the menu. See that the viewpager doesn´t responds to touch events anymore;
Notice that this behavior was reported only on Android 3.2 devices. We have the same application running on 2.x and on 4.x devices, without this problem.
Also, noticed that the Example Application that was downloaded from Google Play doesn´t have this problem.
Does anybody have any advice? Thanks a lot!
Edit 1
Tested on a real device, and confirmed the Behavior. Does anybody have an advice?
I had the same problem and fixed it by using the following work-around.
Replace these lines in SlidingMenu.java:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT < 11) return;
with:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT < 14) return;

Missing function in ViewPager from Android compatibility library

I'm trying to use the function setOffscreenPageLimit() in ViewPager, but Eclipse says The function setOffscreenPageLimit() is undefined for the type ViewPager
However, I see it in the docs and in many examples. ViewPager
Here's my code:
import android.support.v4.view.ViewPager;
public class MyView {
private final ViewPager viewPager;
MyView (ViewPager viewPager) {
this.viewPager = viewPager;
viewPager.setOffscreenPageLimit(2); //<-- can't find it
}
}
I think it must be some strange problems with the library. My minSdk is 7 and target is 13. I've pressed "check for updates" many times and even tried manually re-importing the compatibility library but this function still doesn't show up. Any ideas? I really need this function!
I think that .setOffscreenPageLimit() was a recent addition to the android support library. You should update your support library to the newest (currently v9) using Android SDK Manager. Cheers
I was having a similar problem and the solution was to add the jar file to the libs directory AND add it to the build path in the projects properties. Do not add the file as an external jar, click "add jar".
Hope this helps.

Categories

Resources