如何从片段访问目录中的文件列表?

时间:2019-02-09 10:30:42

标签: android

最后一行给出了Unreachable Statement错误。如何解决?

   public class ListenFragment extends Fragment {

    private String[] files;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup 
container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_listen, container, false);

        files = getActivity().getDir("voices", Context.MODE_PRIVATE).list();

    }
}

2 个答案:

答案 0 :(得分:1)

无法到达的语句错误可以通过这种方式修复:

public class ListenFragment extends Fragment {

    private String[] files;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
        files = getActivity().getDir("voices", Context.MODE_PRIVATE).list();
        return inflater.inflate(R.layout.fragment_listen, container, false);
    }
}

答案 1 :(得分:0)

public class ListenFragment extends Fragment {

    private String[] files;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_listen, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        files = getActivity().getDir("voices", Context.MODE_PRIVATE).list();
    }
}