ImageButton不适用于RecyclerView

时间:2017-03-13 17:36:02

标签: android android-recyclerview imagebutton

当我说imageButton.setOnClickListener时,ImageButton应该只让Toast出现在屏幕上,而应用程序会崩溃。为什么这不起作用? ImageButton的每一行都有一个RecyclerView,所以如果我点击它,我希望从列表中删除该项目。我该怎么做?

    public class FragmentMyList extends Fragment{

    private RecyclerView recyclerView;
    private EditText editTxt;
    private FloatingActionButton btn;
    private ArrayList<Item> itemList;
    private Item item;
    private AdapterItemsRecycler adapterItemsRecycler;
    private ImageButton imageButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View vistaADevolver = inflater.inflate(R.layout.fragment_my_list, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        ItemController itemController = new ItemController();
        editTxt = (EditText)vistaADevolver.findViewById(R.id.editText);
        btn = (FloatingActionButton) vistaADevolver.findViewById(R.id.button);
        recyclerView = (RecyclerView) vistaADevolver.findViewById(R.id.recyclerViewToDo);
        imageButton = (ImageButton)vistaADevolver.findViewById(R.id.delete);

        itemList = new ArrayList<>();
        itemList = itemController.getItemsFromDB(getActivity());
        final InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        adapterItemsRecycler = new AdapterItemsRecycler(itemList, getActivity(), new ItemsListener());

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new SimpleItemDivider(getActivity()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapterItemsRecycler);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ItemController itemController = new ItemController();

                String itemName = editTxt.getText().toString();
                Item item = new Item();
                item.setName(itemName);
                itemController.addItemToDB(item, getActivity());

                // this line adds the data of your EditText and puts in your array
                itemList.add(item);
                inputManager.hideSoftInputFromWindow(getView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                editTxt.getText().clear();
                // next thing you have to do is check if your adapter has changed
                adapterItemsRecycler.notifyDataSetChanged();
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Deleted", Toast.LENGTH_SHORT).show();
            }
        });
        return vistaADevolver;
    }

    class ItemsListener implements AdapterView.OnClickListener{

        @Override
        public void onClick(View view) {

            Item itemTouched = itemList.get(recyclerView.getChildAdapterPosition(view));
            EscuchadorInterface unEscuchador = (EscuchadorInterface)getActivity();
            unEscuchador.itemSelected(itemTouched);

        }
    }
    public interface EscuchadorInterface{
        public void itemSelected(Item anItem);
    }
}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.segunfamisa.sample.bottomnav, PID: 2871
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.segunfamisa.sample.bottomnav/com.segunfamisa.sample.bottomnav.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                  at com.segunfamisa.sample.bottomnav.FragmentMyList.onCreateView(FragmentMyList.java:85)

1 个答案:

答案 0 :(得分:0)

在创建视图时,您只需要扩充布局。这意味着它不会在屏幕上显示,只会在之后显示。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View vistaADevolver = inflater.inflate(R.layout.fragment_my_list, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        return vistaADevolver;

在viewCreated上,这意味着它在屏幕上可见..

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
ItemController itemController = new ItemController();
        editTxt = (EditText)vistaADevolver.findViewById(R.id.editText);
        btn = (FloatingActionButton) vistaADevolver.findViewById(R.id.button);
        recyclerView = (RecyclerView) vistaADevolver.findViewById(R.id.recyclerViewToDo);
        imageButton = (ImageButton)vistaADevolver.findViewById(R.id.delete);

        itemList = new ArrayList<>();
        itemList = itemController.getItemsFromDB(getActivity());
        final InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        adapterItemsRecycler = new AdapterItemsRecycler(itemList, getActivity(), new ItemsListener());

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new SimpleItemDivider(getActivity()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapterItemsRecycler);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ItemController itemController = new ItemController();

                String itemName = editTxt.getText().toString();
                Item item = new Item();
                item.setName(itemName);
                itemController.addItemToDB(item, getActivity());

                // this line adds the data of your EditText and puts in your array
                itemList.add(item);
                inputManager.hideSoftInputFromWindow(getView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                editTxt.getText().clear();
                // next thing you have to do is check if your adapter has changed
                adapterItemsRecycler.notifyDataSetChanged();
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Deleted", Toast.LENGTH_SHORT).show();
            }
        });
        return vistaADevolver;
}

关于生命周期片段的研究。