简体   繁体   中英

Error when I try to implement RecycleView

Hi I was developing an Android App, and I when I try to implement a RecycleView into the logical part of the app, I get A NullPointerException when I call to the DataSet.

I only use the Adapter Class into other class of my App, So I guess that the error should be there.

Here it's my code:

MyAdapter.class


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private static String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public CardView cardView;
        public MyViewHolder(CardView v) {
            super(v);
            cardView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_main_view, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.cardView.setUseCompatPadding(true);
        holder.cardView.setContentPadding(30, 30, 30, 0);
        holder.cardView.setPreventCornerOverlap(true);
        holder.cardView.setCardBackgroundColor(Color.WHITE);
        holder.cardView.setCardElevation(2.1f);
        holder.cardView.setRadius(0);
        holder.cardView.setMaxCardElevation(3f);
        /* TODO: CHARGE THE CONTENT OF EACH CARD */

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
         /**** In the next Line is where I get the NullPointerException  ***/

        return mDataset.length;
    }

    public static String[] getmDataset() {
        return mDataset;
    }
}

MainView.class


public class MainView extends AppCompatActivity {


    //TODO: IMPLEMENTAR LA VISTA PRINCIPAL, DE LA APP.
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_view);

        recyclerView = (RecyclerView) findViewById(R.id.recycleViewServicios);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(MyAdapter.getmDataset());
        recyclerView.setAdapter(mAdapter);




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {


        switch (item.getItemId()) {
            case R.id.item_info:
                Toast miToast = Toast.makeText(getApplicationContext(), "¡Gracias a esta App prodras contratar servicios de manera rápida y segura!", Toast.LENGTH_LONG);
                miToast.setGravity(Gravity.CENTER, 0, 0);
                miToast.show();
                return true;
            case R.id.message_icon:
                Intent miIntent = new Intent(getApplicationContext(), MessageView.class);
                startActivity(miIntent);
                return true;
            case R.id.perfil:
                Intent miIntent2 = new Intent(getApplicationContext(), MainView.class);
                startActivity(miIntent2);
                return true;
            case R.id.item__salir:
                Toast miToast2 = Toast.makeText(getApplicationContext(), "Saliendo de la aplicación", Toast.LENGTH_LONG);
                miToast2.setGravity(Gravity.CENTER, 0, 0);
                miToast2.show();
                System.exit(0);
                return true;
            default:
        return super.onOptionsItemSelected(item);
        }
    }

I'm quite begginer doing serious App, overall into implement the pass of the data between the App.

So if you can me give a hand, take thanks for advance!

You are getting the data from the adapter here:

mAdapter = new MyAdapter(MyAdapter.getmDataset()); //which is null

You should be passing in some dataset instead from the activity, viewModel or whatever.

myDataSet = {"some string", "some other string"}
mAdapter = new MyAdapter(myDataSet);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM