嗨,我在开发一个android应用程序,当我尝试在应用程序的逻辑部分实现一个recycleview时,当我调用数据集时,我得到一个nullpointerexception。
我只在我的应用程序的其他类中使用适配器类,所以我猜错误应该在那里。
这是我的密码:
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);
}
}
我在做一个很严肃的应用程序,全面实现应用程序之间的数据传递。
所以如果你能帮我一把的话,谢谢你的帮助!
1条答案
按热度按时间f1tvaqid1#
您正在从适配器获取以下数据:
您应该传递一些数据集,而不是从activity、viewmodel或其他什么。