--------- beginning of crash
2022-10-13 17:17:26.562 26617-26617/com.example.album E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.album, PID: 26617
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.example.album.ui.Album.FragmentAlbum.onViewCreated(FragmentAlbum.java:32)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8668)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
错误原因:
集合没有初始化,就给她赋值,出现空指针
private List<AlbumEntity>mData;
mData.add(new AlbumEntity("https://img1.baidu.com/it/u=3688400740,2343300667&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313",32,55,true));
mData.add(new AlbumEntity("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fphoto%2F2010-4-2%2Fenterdesk.com-B7215711B63F17326D3C6EDE1D303BDB.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668244460&t=030b9ee00730ed463386f015fa669aeb",152,55,false));
mData.add(new AlbumEntity("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2Fa%2F53c0dd6eab513.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668244461&t=2bf405aefb4881a74c1988ff3810c89e",52,65,false));
mData.add(new AlbumEntity("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F6%2F5822eab200abc.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668244461&t=766899a6e815d7539f397a79bcd10353",153,11,false));
mData.add(new AlbumEntity("https://img0.baidu.com/it/u=191672387,4111662998&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800",24,55,false));
解决办法:
集合数据要先初始化
private List<AlbumEntity>mData = new ArrayList<>();