由于尝试调用虚拟方法'java.lang.String android.content.Context.getPackageName(),因此意图失败 [英] Intent fails because of Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()

查看:445
本文介绍了由于尝试调用虚拟方法'java.lang.String android.content.Context.getPackageName(),因此意图失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的:我有一个带有酒吧,咖啡店图像的RecyclerView.等等

What I have: I have a RecyclerView with images of bars, coffee shops. etc

我想要的是:当您单击这些图像之一时,我会向您显示所选位置的信息,但是在您单击所选图像的那一刻,应用程序崩溃并显示此错误尝试调用虚拟方法'我在其中创建Intent的那一行上的java.lang.String android.content.Context.getPackageName()'在空对象引用上".我已经在寻找这个错误,但我正在按照他们所说的做所有事情

What I want: That when you click on one of these images I show you the info of the selected place, but on the moment that you click the selected image the app crashes and shows this error "Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference" on the line where i create the Intent. I have already looked for this error but i am doing everything as they say

我的问题:我该如何解决?拜托,我非常感谢带有代码的示例,我还不擅长编程,请先谢谢

My question: How do I fix this? Please, I would really appreciate the examples with code, i am not good at programming yet, thanks in advance

我的java类

    public class foodAndGo extends AppCompatActivity {
    ArrayList<ClaseNueva> listalugares;
    RecyclerView recyclerview;

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

       listalugares = new ArrayList<ClaseNueva>();
       recyclerview = (RecyclerView) findViewById(R.id.RecyclerID);
       recyclerview.setLayoutManager(new LinearLayoutManager(this));

       llenarLugares();

       AdapterDatos adapter = new AdapterDatos(foodAndGo.this, listalugares);
       recyclerview.setAdapter(adapter);

    }

    private void llenarLugares() {
        listalugares.add(new ClaseNueva("Restaurantes", R.drawable.carnemejor));
        listalugares.add(new ClaseNueva("Bares", R.drawable.beers));
        listalugares.add(new ClaseNueva("Cafeterías", R.drawable.desayunosmejor));
        listalugares.add(new ClaseNueva("Pizzerías", R.drawable.pizzaamejor));
        listalugares.add(new ClaseNueva("Favoritos", R.drawable.favoritosmejo));

    }
}

我的适配器

    public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolder> {


    ArrayList<ClaseNueva>listalugares;
    Context context;

    public AdapterDatos(foodAndGo foodAndGo, ArrayList<ClaseNueva> listalugares) {
        this.context = context;
        this.listalugares = listalugares;
    }


    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {//quizas poner esto en estatic
        TextView etiNombre;
        ImageView foto;

//        Context context;

        public ViewHolder(View itemView) {
            super(itemView);
//            context = itemView.getContext();

            etiNombre = (TextView) itemView.findViewById(R.id.idNombre);
            foto = (ImageView) itemView.findViewById(R.id.idImagen);
        }
        void setOnClickListeners(){
            foto.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            switch (v.getId()){
//                case R.id.idImagen:
//                    Intent intent = new Intent(context, MapsActivity.class);
//                    context.startActivity(intent);
//                    break;
//            }

        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.etiNombre.setText(listalugares.get(position).getNombre());
        holder.foto.setImageResource(listalugares.get(position).getFoto());

//        holder.setOnClickListeners();
       if (position == 0){
           holder.foto.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent myIntent = new Intent(context, MapsActivity.class);
                   context.startActivity(myIntent);
               }
           });
       }
    }

    @Override
    public int getItemCount() {
        return listalugares.size();
    }

}

推荐答案

我认为您忘记了通过活动传递上下文!

I think you forget to pass context from activity !

AdapterDatos adapter = new AdapterDatos(foodAndGo.this,listalugares);

在Adapter中接收上下文:-

Receive context in Adapter :-

public AdapterDatos(Context context , ArrayList<ClaseNueva> listalugares) {
        this.context = context;
        this.listalugares = listalugares;
    }

希望对您有所帮助!

这篇关于由于尝试调用虚拟方法'java.lang.String android.content.Context.getPackageName(),因此意图失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆