引起:java.lang.NullPointerException:尝试调用虚拟方法'void androidx.recyclerview.widget.RecyclerView.setLayoutManager

问题描述 投票:0回答:1

代码

60 recyclerView = findViewById(R.id.recyclerView);
61 countryAdapter = new CountryAdapter(countryModels);
62 RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity2.this);
63 recyclerView.setLayoutManager(layoutManager); <--- error
64 recyclerView.setAdapter(countryAdapter);

错误

致命异常:主要 进程:com.example.workwithapi,PID:16644 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.workwithapi/com.example.workwithapi.MainActivity2}:java.lang.NullPointerException:尝试调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(空对象引用上的 androidx.recyclerview.widget.RecyclerView$LayoutManager)' 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loopOnce(Looper.java:201) 在 android.os.Looper.loop(Looper.java:288) 在 android.app.ActivityThread.main(ActivityThread.java:7839) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

我们的错误-> 导致:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)” 在 com.example.workwithapi.MainActivity2.ViewData(MainActivity2.java:63) 在 com.example.workwithapi.MainActivity2.onCreate(MainActivity2.java:31) 在 android.app.Activity.performCreate(Activity.java:8051) 在 android.app.Activity.performCreate(Activity.java:8031) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loopOnce(Looper.java:201) 在 android.os.Looper.loop(Looper.java:288) 在 android.app.ActivityThread.main(ActivityThread.java:7839) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

哪里错了?

我想在 TextView 中的 recyclerView 中显示一个字符串数组。 在 findViewById 的帮助下,我将 recyclerView 对象连接到所需的 id,但它给出了 nullException 错误。

android android-recyclerview nullpointerexception
1个回答
0
投票

用这个

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager)
© www.soinside.com 2019 - 2024. All rights reserved.