Reputation: 11931
I am trying to include a SwipeRefreshLayout in my app. It works, but when I go switch my WIFI off (my app uses it and I wanted to include alerts if there is no wifi). Anyone have any suggestion why I get this null pointer exception:
NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1540)
at android.view.View.canScrollVertically(View.java:13757)
at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35)
at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1161)
at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1575)
at android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:643)
at android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:657)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
I have to point out that before adding the SwipeFredreshLayput in my app I did not have such problem.
This is my XML: Hourly_Fragment:
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/bg_gradient"
tools:context="koemdzhiev.com.stormy.ui.HourlyForecastActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/hourly_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="@+id/recyclerView"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
This is my Hourly_fragment java code:
public class Hourly_forecast_fragment extends Fragment {
private Hour[] mHours;
private MainActivity mActivity;
@InjectView(R.id.hourly_swipe_refresh_layout)
SwipeRefreshLayout mSwipeRefreshLayout;
RecyclerView.LayoutManager layoutManager;
//inject the RecyclerView as member variable
@InjectView(R.id.recyclerView)
RecyclerView mRecyclerView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = ((MainActivity) getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.hourly_forecast_fragment,container,false);
ButterKnife.inject(this, v);
mSwipeRefreshLayout.setColorSchemeColors(R.color.orange,R.color.green,R.color.blue);
if (mRecyclerView != null)
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (mActivity.isNetworkAvailable()) {
mActivity.getLocation();
} else {
mActivity.alertForNoInternet();
mSwipeRefreshLayout.setRefreshing(false);
}
}
});
Log.e("Forecast_fragment", "onCreateView");
return v;
}
public void setUpHourlyFragment(){
if (mActivity.mForecast != null) {
Hour[] hourlyForecast = mActivity.mForecast.getHourlyForecast();
mHours = Arrays.copyOf(hourlyForecast, hourlyForecast.length, Hour[].class);
HourAdapter adapter = new HourAdapter(mActivity, mHours);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(mActivity);
mRecyclerView.setLayoutManager(layoutManager);
//if dealing with fixed size data, it is recommended to do the following...
mRecyclerView.setHasFixedSize(true);
}
}
}
Main activity code:
public class MainActivity extends AppCompatActivity {
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Current","Hourly","Daily"};
int Numboftabs =3;
Current_forecast_fragment mCurrent_forecast_fragment;
Hourly_forecast_fragment mHourly_forecast_fragment;
Daily_forecast_fragment mDaily_forecast_fragment;
public static final String TAG = MainActivity.class.getSimpleName();
public static final String LOCATION_KEY = "location_key";
public Forecast mForecast;
public static final String DAILY_FORECAST = "DAILY_FORECAST";
public static final String HOURLY_FORECAST = "HOURLY_FORECAST";
//default coordinates - Gotse Delchev, UK Lati:57.156866 ; Long:
private double latitude = 41.5667;
private double longitude = 23.7333;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------MY CODE STARTS HERE-----------------
changeWindowTopColor();
this.mCurrent_forecast_fragment = new Current_forecast_fragment();
this.mHourly_forecast_fragment = new Hourly_forecast_fragment();
this.mDaily_forecast_fragment = new Daily_forecast_fragment();
getLocation();
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs,mCurrent_forecast_fragment,
mHourly_forecast_fragment,mDaily_forecast_fragment);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(3);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return ContextCompat.getColor(MainActivity.this,R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
private void getForecast(double latitude, double longitude) {
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecast)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrent_forecast_fragment.toggleRefresh();
mHourly_forecast_fragment.mSwipeRefreshLayout.setRefreshing(false);
}
});
alertUserAboutError();
}
//when the call to the Okhttp library finishes, than calls this method:
@Override
public void onResponse(Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrent_forecast_fragment.toggleRefresh();
mHourly_forecast_fragment.mSwipeRefreshLayout.setRefreshing(false);
}
});
try {
String jsonData = response.body().string();
//Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mForecast = parseForecastDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrent_forecast_fragment.updateDisplay();
mHourly_forecast_fragment.setUpHourlyFragment();
mDaily_forecast_fragment.setUpDailyFragment();
}
});
} else {
alertUserAboutError();
}
} catch (IOException | JSONException e) {
Log.e(TAG, "Exception caught:", e);
}
}
});
} else {
mCurrent_forecast_fragment.toggleRefresh();
mHourly_forecast_fragment.mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(this,getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
alertForNoInternet();
}
}
public void alertForNoInternet() {
WIFIDialogFragment dialog = new WIFIDialogFragment();
dialog.show(getFragmentManager(), getString(R.string.error_dialog_text));
}
Upvotes: 2
Views: 7493
Reputation: 109
if you are using kotlin then better check the null value before execution. for example:
val reachBottom = recyclerView?.canScrollVertically(1)
if (reachBottom == false) {
//scroll view is at bottom
//your code
}
Upvotes: 0
Reputation: 7082
Put these lines:
layoutManager = new LinearLayoutManager(mActivity);
mRecyclerView.setLayoutManager(layoutManager);
after this line in onCreateView:
ButterKnife.inject(this, v);
You should generally avoid having a RecyclerView
without a LayoutManager
attached, it leads to weird exceptions like this one, as most of the RecyclerView
features, even non-drawing-related rely on the LayoutManager
. It is however completely fine to have a RecyclerView
without an Adapter
(because you do net have the data yet or whatever). But you should attach a LayoutManager
asap after creating the RecyclerView
:-)
Upvotes: 2