我在片段中有一个runnable。 runnable用于更新文本视图以及使用按钮接收输入。但是程序一旦进入runnable就不会进入。
请帮忙。我究竟做错了什么。 谢谢。 代码如下。我在runnable中有按钮和textview。
public class TodayFragment extends Fragment {
//initialisations
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//UI initialisations
Handler mHandler = new Handler();
Runnable mTicker = new Runnable() {
@Override
public void run() {
//user interface interactions and updates on screen
mHandler.postDelayed(mTicker, 1000);
mTicker.run();
}
答案 0 :(得分:2)
似乎你想每1秒改变一下你的观点。如果你这样说,你应该在runnable方法之外执行你的处理程序。即:
Handler mHandler = new Handler();
Runnable mTicker = new Runnable() {
@Override
public void run() {
// user interface interactions and updates on screen
// if you want to run this handler only once then delete below line
mHandler.postDelayed(mTicker, 1000);
}
};
mHandler.postDelayed(mTicker, 1000);
答案 1 :(得分:1)
尝试此更改
mTicker = new Runnable() {
public void run() {
//user interface interactions and updates on screen
mHandler.postDelayed(mTicker, 1000);
}
};
mTicker.run();
答案 2 :(得分:1)
这对我有用。
import 'package:flutter/material.dart';
import 'package:flutter_vlc_player/flutter_vlc_player.dart';
void main() {runApp(MyApp());}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Raspberry Pi 4 stream'),
);}}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _streamUrl;
VlcPlayerController _vlcPlayerController;
@override
void iniState(){
super.initState();
_vlcPlayerController = new VlcPlayerController();
}
void _incrementCounter() {
setState(() {
_streamUrl = 'http://192.168.1.14:8081';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title),),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_streamUrl == null
? new Container(
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'stream closed',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
background: Paint()..color = Colors.red,
),
)
]
),),): new VlcPlayer(
defaultHeight: 480,
defaultWidth: 640,
url: _streamUrl,
controller: _vlcPlayerController,
placeholder: Container(),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.play_arrow),
),);}}