onCreateView()中无法访问的语句

时间:2017-12-24 18:33:24

标签: java android android-fragments return

我正在尝试使用多个片段构建一个应用程序,其中所有片段都实现LoaderCallbacks。我一直在阅读 StackOverflow 上的帖子,其中大多数人指出要使用import android.support.v4.app的东西。而我做到了。但是,当我在片段的getLoaderManager内尝试onCreateView时,我收到无法访问的语句错误。

这是我的 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) {

    }
}

这是我的Loader课程,

  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 个答案:

答案 0 :(得分:2)

因为在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);
}