tries to prevent a crash in the custom ViewPager

master
y20k 2017-01-16 15:56:58 +01:00
parent 6ddcf3ae67
commit 93176e2a7a
3 changed files with 24 additions and 4 deletions

View File

@ -23,6 +23,7 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'

View File

@ -175,6 +175,17 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
startActivity(aboutIntent);
return true;
// CASE MY LOCATION
case R.id.action_bar_my_location:
LogHelper.v(LOG_TAG, "MainActivity: CASE MY LOCATION."); // todo remove
if (mSelectedTab != FRAGMENT_ID_MAP) {
LogHelper.v(LOG_TAG, "MainActivity: ."); // todo remove
// show map fragment
mSelectedTab = FRAGMENT_ID_MAP;
mViewPager.setCurrentItem(mSelectedTab);
}
return false;
// CASE DEFAULT
default:
return super.onOptionsItemSelected(item);
@ -215,6 +226,9 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
super.onDestroy();
LogHelper.v(LOG_TAG, "onDestroy called.");
// reset selected tab
mSelectedTab = FRAGMENT_ID_MAP;
// disable broadcast receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(mTrackingStoppedReceiver);
}
@ -264,7 +278,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
@ -321,11 +334,13 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
case FRAGMENT_ID_MAP:
// show the Floating Action Button
mFloatingActionButton.show();
mSelectedTab = FRAGMENT_ID_MAP;
break;
case FRAGMENT_ID_TRACK:
// hide the Floating Action Button - and its sub menu
mFloatingActionButton.hide();
showFloatingActionButtonMenu(false);
mSelectedTab = FRAGMENT_ID_TRACK;
break;
default:
// show the Floating Action Button

View File

@ -73,10 +73,14 @@ public class NonSwipeableViewPager extends ViewPager {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
if (scroller != null) {
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} else {
LogHelper.v(LOG_TAG, "Unable to get mScroller field."); // todo remove
}
} catch (Exception e) {
LogHelper.e(LOG_TAG, "Problem accessing or modifying the mScroller field.");
LogHelper.e(LOG_TAG, "Problem accessing or modifying the mScroller field. Exception: " + e);
e.printStackTrace();
}
}