Unreachable statement in onCreateView()
Unreachable statement in onCreateView()
I am trying to build an app with multiple fragments where all the fragments implement LoaderCallbacks
. I've been reading posts on StackOverflow where most of them were pointing out to use import android.support.v4.app
stuff. And I did. However, when I try to getLoaderManager
from within the onCreateView
of the fragment, I am getting an Unreachable statement error.
LoaderCallbacks
import android.support.v4.app
getLoaderManager
onCreateView
This is one of my Fragments:
package com.example.android.morenews;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by on 24.12.17.
*/
public class CultureFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<News>>{
private String baseUrlString= "https://content.guardianapis.com/search?show-fields=headline,thumbnail,wordcount,trailText";
private String LOG_TAG= CultureFragment.class.getName();
public LoaderManager loaderManager;
public Fragment fragment = this;
public CultureFragment(){
//empty constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
loaderManager = getLoaderManager(); <-- This is where im getting the unreachable statement error.
loaderManager.initLoader(1, null, this);
}
@Override
public Loader<List<News>> onCreateLoader(int id, Bundle args) {
Uri baseUri = Uri.parse(baseUrlString);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("section","sports");
uriBuilder.appendQueryParameter("page-size","20");
uriBuilder.appendQueryParameter("api-key","test");
uriBuilder.appendQueryParameter("format","json");
uriBuilder.appendQueryParameter("orderBy", "newest");
Log.e(LOG_TAG, " the url " + uriBuilder.toString());
return new NewsLoader(getContext(), uriBuilder.toString());
}
@Override
public void onLoadFinished(android.support.v4.content.Loader<List<News>> loader, List<News> data) {
}
@Override
public void onLoaderReset(android.support.v4.content.Loader<List<News>> loader) {
}
}
And this is my Loader class,
package com.example.android.morenews;
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import java.util.List;
/**
* Created by on 24.12.17.
*/
public class NewsLoader extends AsyncTaskLoader<List<News>> {
String mUrl;
public NewsLoader(Context context, String url)
{
super(context);
mUrl=url;
}
@Override
public List<News> loadInBackground() {
if (mUrl==null){
return null;
}
return QueryUtils.fetchNewsFromApi(mUrl); }
@Override
protected void onStartLoading() {
forceLoad();
}
}
1 Answer
1
It's because of calling it after the return
statement which means no other code lines will be executed. Do it before the return
statement:
return
return
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
loaderManager = getLoaderManager();
loaderManager.initLoader(1, null, this);
return super.onCreateView(inflater, container, savedInstanceState);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.