我在标签片段中更新我的 TextView 时遇到了麻烦。我想用全局变量数据每秒在 TextView 上设置文本。这在活动中效果很好,但在片段中效果不佳。错误是:textView2 无法解析。如果有更好的更新方法,或者这不好,请告诉我。
public class FragmentA extends Fragment {
//timer
private TimerTask mTimerTask;
private Timer timer = new Timer();
private final Handler timerHandler = new Handler();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
//TextView for Device name
TextView textView3 = (TextView)myFragmentView.findViewById(R.id.TextView3);
//TextView for Serial number
TextView textView5 = (TextView)myFragmentView.findViewById(R.id.TextView5);
//TextView for Software version
TextView textView7 = (TextView)myFragmentView.findViewById(R.id.TextView7);
onTimerTick();
timer.schedule(mTimerTask, 10, 1000);
return myFragmentView;
}
//timer tick on every 1 s
public void onTimerTick() {
mTimerTask = new TimerTask() {
//this method is called every 1ms
public void run() {
timerHandler.post(new Runnable() {
public void run() {
//update textView
//ERROR:textView2 cannot be resolved
textView2.setText(m_info.deviceName);
Log.d("tag", "Hello from timer fragment");
}
});
}};
}
}