无法解析符号newInstance

时间:2018-05-15 07:44:09

标签: android android-fragments

@Override
    public Fragment getItem(int i) {
        if (i == 0) {
            return  new CryptoListChildFragment.newInstance();
        }else {
            return  new CryptoListChildFavFragment.newInstance();
        }
    }  


public class CryptoListChildFragment extends Fragment {
private RecyclerView mRecyclerView;
private ProgressBar mProgressBar;
private CoinAdapter mAdapter;
private NetworkService retrofit;
private Coin[] coinList;
private FrameLayout root;

public CryptoListChildFragment(){

}

public static CryptoListChildFragment newInstance() {
    CryptoListChildFragment myFragment = new CryptoListChildFragment();
    return myFragment;
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_crypto_list_child, container, false);
    mRecyclerView = view.findViewById(R.id.coin_list);
    mProgressBar = view.findViewById(R.id.progress_bar_coins);
    root = view.findViewById(R.id.main_view);
    return view;
}

我有newInstance()方法,但它说'无法解析符号newInstance'。我试图从FragmentPageAdapter中调用它。我创建了一个新的Fragment,同样再次出现同样的错误。

1 个答案:

答案 0 :(得分:2)

必须是:

....
if (i==1){
   return CryptoListChildFragment.newInstance()
....

顺便说一句: 你不应该在这里创建一个新变量:

public static CryptoListChildFragment newInstance() {
    CryptoListChildFragment myFragment = new CryptoListChildFragment();
    return myFragment;
}

在一行中返回实例:

public static CryptoListChildFragment newInstance() {
    return new CryptoListChildFragment();
}