尝试调用虚拟方法' android.view.view androidx.recyclerview.widget.recyclerview.findviewbyid(int)'在null对象参考上

发布于 2025-01-26 19:01:51 字数 3808 浏览 3 评论 0原文

我找不到为什么该应用程序继续崩溃。

这里我的主要活动

public class MainActivity extends AppCompatActivity {
List<String> catName;
List<Integer> catImages;

RecyclerView recyclerView;

String selectItem;

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

    recyclerView.findViewById(R.id.recycleview);

    catName = new ArrayList<>();
    catImages = new ArrayList<>();

    catName.add("Farina");
    catName.add("Farina inverso");
    catName.add("Alcol");
    catName.add("Panna");
    catName.add("Cioccolato");

    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);

    GridLayoutManager gridLayoutManager = new GridLayoutManager
            (this,2,GridLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setHasFixedSize(true);

    categoryCardViewAdpt adapter = new categoryCardViewAdpt(this,catName,catImages);
    recyclerView.setAdapter(adapter);

在这里我的适配器。

public class categoryCardViewAdpt extends   RecyclerView.Adapter<categoryCardViewAdpt.ViewHolder> {
List<String> title1;
List<Integer> image1;
LayoutInflater inflater;


public categoryCardViewAdpt (Context context, List<String> catName, List<Integer> catImage){
    this.title1 = catName;
    this.image1 = catImage;
    this.inflater = LayoutInflater.from(context);
    //this.context = context;

}

public class ViewHolder extends RecyclerView.ViewHolder{
    ImageView catImageView;
    TextView catTv;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        catImageView = itemView.findViewById(R.id.category_img2);
        catTv = itemView.findViewById(R.id.category_name_2);
    }
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.card_view_layout,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull categoryCardViewAdpt.ViewHolder holder, int position) {

    holder.catTv.setText(title1.get(position));
    holder.catImageView.setImageResource(image1.get(position));
}

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

,在这里我的布局文件

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycleview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

,在这里我得到的错误:

java.lang.lang.runtimeexception:无法启动活动ComponentInfo {空对象引用 at android.app.activitythread.performlaunchactivity(activityThread.java:3449) 在android.app.act.activitythread.handlelaunchactivity(activityThread.java:3601) at android.app.servertransaction.launchactivityItem.execute(启动activityItem.java:85) at android.app.servertransaction.transactionexecutor.executecallbacks(transactionexecutor.java:135) 在Android.app.servertransaction.transactionexecutor.execute(TransActionExecutor.java:95) at android.app.ActivityThread $ H.Handlemessage(activityThread.java:2066) at android.os.handler.dispatchmessage(Handler.java:106)

I can't not find why the app continue to crash.

Here my Main Activity

public class MainActivity extends AppCompatActivity {
List<String> catName;
List<Integer> catImages;

RecyclerView recyclerView;

String selectItem;

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

    recyclerView.findViewById(R.id.recycleview);

    catName = new ArrayList<>();
    catImages = new ArrayList<>();

    catName.add("Farina");
    catName.add("Farina inverso");
    catName.add("Alcol");
    catName.add("Panna");
    catName.add("Cioccolato");

    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);
    catImages.add(R.drawable.ic_baseline_fastfood_24);

    GridLayoutManager gridLayoutManager = new GridLayoutManager
            (this,2,GridLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setHasFixedSize(true);

    categoryCardViewAdpt adapter = new categoryCardViewAdpt(this,catName,catImages);
    recyclerView.setAdapter(adapter);

Here my Adapter.

public class categoryCardViewAdpt extends   RecyclerView.Adapter<categoryCardViewAdpt.ViewHolder> {
List<String> title1;
List<Integer> image1;
LayoutInflater inflater;


public categoryCardViewAdpt (Context context, List<String> catName, List<Integer> catImage){
    this.title1 = catName;
    this.image1 = catImage;
    this.inflater = LayoutInflater.from(context);
    //this.context = context;

}

public class ViewHolder extends RecyclerView.ViewHolder{
    ImageView catImageView;
    TextView catTv;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        catImageView = itemView.findViewById(R.id.category_img2);
        catTv = itemView.findViewById(R.id.category_name_2);
    }
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.card_view_layout,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull categoryCardViewAdpt.ViewHolder holder, int position) {

    holder.catTv.setText(title1.get(position));
    holder.catImageView.setImageResource(image1.get(position));
}

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

And here my layout file

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycleview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

and here the Error i get:

java.lang.RuntimeException: Unable to start activity ComponentInfo{g.bruno.crocesandrea/g.bruno.crocesandrea.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2025-02-02 19:01:51

您只需要更改

recyclerView.findViewById(R.id.recycleview);

recyclerView=findViewById(R.id.recycleview);

You just need to change

recyclerView.findViewById(R.id.recycleview);

into

recyclerView=findViewById(R.id.recycleview);
变身佩奇 2025-02-02 19:01:51

我发现,
更改代码

recyclerView.findViewById(R.id.recycleview);

在您的on创作方法中,您必须从中

recyclerView = recyclerView.findViewById(R.id.recycleview);

I found that,
In your onCreate method you must change your code from

recyclerView.findViewById(R.id.recycleview);

into

recyclerView = recyclerView.findViewById(R.id.recycleview);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文