我试图在recyclerView中显示数据,但是我的应用程序崩溃了
公共类Search_book_admin扩展了AppCompatActivity {
ArrayList<Model_class> modelClassArrayList;
myAdapert myadapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = (RecyclerView) findViewById(R.id.rv);
setContentView(R.layout.activity_search_book_admin);
modelClassArrayList = new ArrayList<>();
AddItems();
myadapter = new myAdapert(modelClassArrayList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myadapter);
}
private void AddItems()
{
try {
for(int i=0;i<20;i++)
{
modelClassArrayList.add(new Model_class(R.drawable.ic_action_name,
"name: "+i,
"email"+i));
}
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBackPressed() {
finish();
}
((我的适配器类))
公共类myAdapert扩展了RecyclerView.Adapter { ArrayList modelClassArrayList;
public myAdapert(ArrayList<Model_class> modelClassArrayList) {
this.modelClassArrayList = modelClassArrayList;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View customView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.custom_row,viewGroup,false);
myViewHolder myviewHolder = new myViewHolder(customView);
return myviewHolder;
}
@Override
public void onBindViewHolder(@NonNull myViewHolder myViewHolder, int i) {
Model_class model_class = modelClassArrayList.get(i);
myViewHolder.contactPic.setImageResource(model_class.getImage());
myViewHolder.contactName.setText(model_class.getName());
myViewHolder.contectEmail.setText(model_class.getEmail());
}
@Override
public int getItemCount() {
return modelClassArrayList.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
ImageView contactPic;
TextView contactName,contectEmail;
public myViewHolder(@NonNull View itemView) {
super(itemView);
contactPic = itemView.findViewById(R.id.image);
contactName = itemView.findViewById(R.id.name);
contectEmail = itemView.findViewById(R.id.email);
}
}
2019-06-15 17:39:16.114 6337-6337 /? E / ProActivieLaunch:发送广播 2019-06-15 17:39:16.121 4466-4702 /? E / ActivityManager:接收到TouchDown意图,启动ActiveLaunch 2019-06-15 17:39:16.139 29048-29048 /? E /合子:isWhitelistProcess-进程被列入白名单 2019-06-15 17:39:16.140 29048-29048 /? E /合子:accessInfo:1 2019-06-15 17:39:16.563 4010-4053 /? E / BufferQueueProducer:[com.sec.android.app.launcher / com.sec.android.app.launcher.activities.LauncherActivity [6337]#0]断开连接:未连接(req = 1) 2019-06-15 17:39:16.566 4466-6303 /? E / WindowManager:win = Window {cdfa8e6 u0 com.sec.android.app.launcher / com.sec.android.app.launcher.activities.LauncherActivity} destroySurfaces:appStopped = false win.mWindowRemovalAllowed = false win.mRemoveOnExit = false win .mViewVisibility = 8调用者= com.android.server.wm.WindowManagerService.tryStartExitingAnimation:2748 com.android.server.wm.WindowManagerService.relayoutWindow:2446 com.android.server.wm.Session.relayoutForTranslate:287 android.view.IWindowSession $ Stub.onTransact:432 com.android.server.wm.Session.onTransact:186 android.os.Binder.execTransact:739 2019-06-15 17:39:17.376 4466-4537 /? E / WindowManager:win = Window {7d266dd u0启动画面com.example.library_management_system EXITING} destroySurfaces:appStopped = false win.mWindowRemovalAllowed = true win.mRemoveOnExit = true win.mViewVisibility = 0 caller = com.android.server.wm.AppWindowToken .destroySurfaces:870 com.android.server.wm.AppWindowToken.destroySurfaces:851 com.android.server.wm.WindowState.onExitAnimationDone:5423 com.android.server.wm.WindowStateAnimator.onAnimation完成:319 com.android.server.wm .WindowState.onAnimationFinished:5845 com.android.server.wm .- $$ Lambda $ yVRF8YoeNdTa8GR1wDStVsHu8xM.run:2 com.android.server.wm.SurfaceAnimator.lambda $ getFinishedCallback $ 0:100 2019-06-15 17:39:19.682 29048-29048 / com.example.library_management_system E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.example.library_management_system,PID:29048 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.library_management_system / com.example.library_management_system.Search_book_admin}:java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v7.widget.RecyclerView。 setHasFixedSize(boolean)'在空对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3115) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3258) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1955) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7058) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' 在com.example.library_management_system.Search_book_admin.onCreate(Search_book_admin.java:25) 在android.app.Activity.performCreate(Activity.java:7327) 在android.app.Activity.performCreate(Activity.java:7318) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3095) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3258) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1955) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7058) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 2019-06-15 17:39:19.769 29101-29101 /? E / Zygote:isWhitelistProcess-进程被列入白名单
答案 0 :(得分:0)
将此代码粘贴到onCreate方法中
setContentView(R.layout.activity_search_book_admin);
recyclerView = (RecyclerView) findViewById(R.id.rv);
modelClassArrayList = new ArrayList<>();
AddItems();
myadapter = new myAdapert(modelClassArrayList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myadapter);