在onCreateView中初始化RecyclerView和Adapter,但收到错误"没有附加适配器;跳过布局"

时间:2016-05-17 18:56:17

标签: java android android-layout android-recyclerview

我收到错误"没有附加适配器;跳过布局"使用RecyclerView时。我在Stack Overflow上查看了很多答案,但他们似乎都说必须初始化RecyclerView和Adapter。我这样做,但仍然得到这个错误。谁知道为什么?

public class HomeFragment extends Fragment {

private EditText ipText;
private VncDatabase database;
private GridLayoutManager lLayout;

private ButtonCreator bc;
RecyclerViewAdapter rcAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    List<ItemObject> myList = new ArrayList<>();
    rcAdapter = new RecyclerViewAdapter(getActivity(),myList);


}

// onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.home_fragment, container, false);


    // Get screen to determine number of rows
    // see src/main/res/values/integers.xml
    int columnCount = getResources().getInteger(R.integer.grid_column_count);
    lLayout = new GridLayoutManager(getActivity(), columnCount,
            GridLayoutManager.HORIZONTAL, false);

    RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);

    rView.setHasFixedSize(true);
    rView.setLayoutManager(lLayout);
    rView.setAdapter(rcAdapter);


    return view;
}



public void createButton (Drawable d, String appName){

    Log.d("tag_name", "Make sure createButton is called");
    rcAdapter.addItem(new ItemObject(appName, d));

}



// In order to get the view of HomeFragment correctly

public interface ButtonCreator{
    void buttonCreator(Drawable d, String string);
}



@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    bc = (ButtonCreator) activity;
}


}

我的drawable和字符串从另一个调用&#34; createButton&#34;方法,它将一个Image按钮添加到我的GridLayout。

0 个答案:

没有答案