----------------------------------------
[20.06.28]更新
成功了謝謝各位大神的指導,小弟感激不盡。這次問題的重點是關於fragment的技巧,若是先了解就不會有這種問題。
----------------------------------------
照著這個影片做完後https://www.youtube.com/watch?v=BrDX6VTgTkg
出現以下錯誤,應用閃退
2020-06-26 16:03:49.125 2559-2559/? E/om.example.fsr: Unknown bits set in runtime_flags: 0x8000
2020-06-26 16:03:53.292 2559-2559/com.example.fsrc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fsrc, PID: 2559
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fsrc/com.example.fsrc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.fsrc.MainActivity.onCreate(MainActivity.java:170)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
將以下程式註解後,繼續根據logcat註解出現錯誤的程式
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//recyclerView.setLayoutManager(layoutManager);
//recyclerView.setHasFixedSize(true);
最後註解以下程式後恢復正常,但等於沒使用此功能,即無錯誤
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), messagesList);
//recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
小弟求(詳)解~~~
1.跟著影片自己輸入程式碼很容易有誤差,最近三週有人有類似的問題Recyclerview: No adapter attached; skipping layout
作者回答「use glide latest version」
作者回答「Please update your sdk and implement the latest firebase sdk」
2.以錯誤訊息和你註解後就不會錯的程式碼來看,問題是recyclerView = findViewById(R.id.recyclerView);
的 recyclerView 是 null
以致之後的 setLayoutManager 產生 Exception 而閃退
你得自己找出為什麼
找出這個原因之後,也不保證不會有其他錯誤
3.想想一個問題,對你而言,學會這個影片的內容值多少錢?
心裡有答案之後再點付費下載影片範例App(含程式碼)