gpt4 book ai didi

java - 使用定时器时java中的"Variable might not have been initialized"

转载 作者:行者123 更新时间:2023-12-01 20:19:58 25 4
gpt4 key购买 nike

这是我在 fragment 中的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view= inflater.inflate(R.layout.fragment_profile, container, false);
profile();
textView = (TextView) view.findViewById(R.id.textViewUsername);
listView = (ListView) view.findViewById(R.id.listView);
timer_start();
return view;
}

public void timer_start(){
final Runnable mTicker = new Runnable() {
@Override
public void run() {
sendRequest();
handler.postDelayed(mTicker, 5000); // error shows only for this line
}
};

handler.postDelayed(mTicker, 5000);
}

我想每5秒执行一次sendRequest()函数。但它显示错误:“当我调用timer_start() fragment 时,变量mTicker可能尚未初始化。

最佳答案

您在初始化的同一行中引用了 mTicker。这是不允许的。这就像说:

String s = s;

这没有意义。尝试使用“this”:

handler.postDelayed(this, 5000);

关于java - 使用定时器时java中的"Variable might not have been initialized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com