使用计时器时在 Java 中"Variable might not have been initialized"



这是我的代码片段:

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(( 函数。但它显示错误:"变量 mTicker 可能尚未初始化",而我正在调用 timer_start(( 片段。

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

String s = s;

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

handler.postDelayed(this, 5000);

相关内容

最新更新