問題描述
當(dāng)我在 menu1_Fragment.java
的 onCreateView
中放置一個(gè)函數(shù)時(shí)發(fā)生錯(cuò)誤.提前抱歉,因?yàn)槲乙矔?huì)把我的日志放進(jìn)去,所以這可能會(huì)使我的帖子有點(diǎn)大,所以我會(huì)告訴你布局.我很抱歉有很多問題.
Error occurs when I place a function inside onCreateView
in menu1_Fragment.java
. Sorry in advance as I will be placing my logs in as well so it might make my post a bit big so I will tell you the layout. I am new sorry for the loads of questions.
如果我不將函數(shù) setClick()
放在我的片段中,則代碼可以完美運(yùn)行.快速注意該功能基本上使我在 menu1_layout.xml
中的圖像成為一個(gè) 3 向按鈕,即開始停止重置,當(dāng)您重置時(shí),它會(huì)彈出一個(gè)準(zhǔn)確時(shí)間的祝酒詞
The code works perfectly if I just don't put the function setClick()
inside my fragment. quick note the function basically makes my image in menu1_layout.xml
a 3 way button aka start stop reset where when you reset, it pops up a toast of exact time you got
1) menu1_Fragment.java
2) 日志
3) MyActivity.Java
4) Menu1_layout.xml
推薦答案
你不能使用 onCreateView()
中的 getActivity()
因?yàn)槠蔚幕顒?dòng)還沒有被尚未創(chuàng)建.在調(diào)用 onActivityCreated()
之前,您不應(yīng)該使用 getActivity()
,或者更好的是,使用您膨脹的 rootview
來執(zhí)行您的 findViewByID()
調(diào)用.
You can't use getActivity()
from onCreateView()
as the fragment's activity has not been created yet. You should use not use getActivity()
until onActivityCreated()
is called, or better yet, use the rootview
you inflated to do your findViewByID()
calls.
另外,您正在初始化按鈕 3 次.你應(yīng)該擺脫其中兩個(gè):)
Also, you are initializing your button three times. You should get rid on two of them :)
這篇關(guān)于片段中的功能使模擬器崩潰的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!