gpt4 book ai didi

java - onCreateView() 中无法访问的语句

转载 作者:太空狗 更新时间:2023-10-29 13:05:58 25 4
gpt4 key购买 nike

我正在尝试构建一个包含多个 fragment 的应用程序,其中所有 fragment 都实现了 LoaderCallbacks。我一直在阅读 StackOverflow 上的帖子,其中大多数都指出要使用 import android.support.v4.app 东西。我做到了。但是,当我尝试从 fragment 的 onCreateViewgetLoaderManager 时,我收到了 Unreachable statement 错误。

这是我的一个 fragment :

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();
}
}

最佳答案

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

关于java - onCreateView() 中无法访问的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47963134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com